Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML PHP - Changing Element Attributes

Status
Not open for further replies.

MartinCoxxx

Technical User
Apr 27, 2006
1
GB
I've been trying to create a PHP / XML shopping cart system. So far, so good. The way I have this working is; when the customer clicks the 'add to basket' link, the page sends the values of the variables via URL to another PHP page which will generate an XML document.

Here's the PHP document that generates an XML document:

<?php
if (file_exists($_GET['session'].'.xml')) {
$doc = domxml_open_file($_GET['session'].'.xml');

$root = $doc->document_element();

$child1 = $root->child_nodes();

foreach ($child1 as $children) {
if ($children->get_attribute("id") == '001MOC') {
$qty = $children->get_attribute("qty") + $_GET['qty'];
$children->set_attribute("qty", "$qty");
exit;
}
}

// add children to the root
$child = $root->new_child("item");

$child->set_attribute("id", $_GET['item_code']);

$child->set_attribute("qty", $_GET['qty']);

// add children to the item
$child1 = $child->new_child("session", $_GET['session']);

// add children to the item
$child1 = $child->new_child("code", $_GET['item_code']);

// add children to the item
$child1 = $child->new_child("desc", $_GET['item_desc']);

// add children to the item
$child1 = $child->new_child("base_price", $_GET['base_price']);

// add children to the item
$child1 = $child->new_child("discount", $_GET['discount']);

// dump the tree as a string
$doc->dump_file($_GET['session'].'.xml', false);

} else {

// create DomDocument object
$doc = new_xmldoc("1.0");

// add root node
$root = $doc->add_root("items");

// add children to the root
$child = $root->new_child("item");

$child->set_attribute("id", $_GET['item_code']);

$child->set_attribute("qty", $_GET['qty']);

// add children to the item
$child1 = $child->new_child("session", $_GET['session']);

// add children to the item
$child1 = $child->new_child("code", $_GET['item_code']);

// add children to the item
$child1 = $child->new_child("desc", $_GET['item_desc']);

// add children to the item
$child1 = $child->new_child("base_price", $_GET['base_price']);

// add children to the item
$child1 = $child->new_child("discount", $_GET['discount']);

// dump the tree as a string
$doc->dump_file($_GET['session'].'.xml', true);

}

?>


This code checks to see if the document exists, if it doesn't, it will create the new document, add 'items' as the root and create a child 'item' with the product information inside. If it does exist it just adds another child to the root 'items'.

The problem I am trying to solve is;
I need the PHP script to check if an item already exists in the XML page. And if so, rather than adding another 'item' element; update the quantity of the existing one.

I've kind of got this working in the script, but instead of updating the quantity of the existing item, it adds another one but with the new quantity. So all i need to do is update the quantity of the existing item rather than add a new item with the new quantity. Sorry about the long winded explanation (n00b).

Thanks in advance!

Martin
 
Martin, welcome to Tek-Tips.

Since you found the TGML tags, check out the [ignore]
Code:
Program code here
[/ignore] tag which produces the following:
Code:
Program code here
This makes your code more readable by choosing a monospaced font, placing the code in a box, and preserving your indentations.

I am not expert in the DOM. Since you are setting an id attribute perhaps you can test the results of a get_item_by_id. Otherwise, it would seem that an XPath expression is in your future.

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top