MartinCoxxx
Technical User
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
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