Change the value of XML file using PHP - certain child

Hello everybody! My problem is as follows: I am using an xml file with products info. Here it is - example.xml

<?xml version="1.0" encoding="ISO-8859-1"?> <products> <item> <name>Flat Screen Television SONY KDL-4500WEED</name> <image>http://img.zap.co.il/pics/2/7/9/2/37682972c.gif</image> <description>This is our newest TV set from the SONY Comp. and we hope you buy it mothefuckerzzzz....zasdhaskjdhkahdca a dsah dha adlasldoao al ladjjada</description> <left>17</left> </item> <item> <name>Aiwa ozvuchitelna sistema</name> <image>http://crev.vo.llnwd.net/o42/audioreview/images/products/product_119021.jpg</image> <description>This is our newest TV set from the Aiwa Comp. and we hope you buy it mothefuckerzzzz....zasdhaskjdhkahdca a dsah dha adlasldoao al ladjjada</description> <left>12</left> </item> <item> <name>Blu-Ray DVD Player Panasonic</name> <image>http://news.cnet.com/i/bto/20070725/BD-UP5000_overhead.jpg</image> <description>This is our newest TV set from the Aiwa Comp. and we hope you buy it mothefuckerzzzz....zasdhaskjdhkahdca a dsah dha adlasldoao al ladjjada</description> <left>33</left> </item> <item> <name>DURO na SHISH...</name> <image>http://media.otkrovenia.com/profiles/DureFF.jpg</image> <description>This is our newest TV set from the Aiwa Comp. and we hope you buy it mothefuckerzzzz....zasdhaskjdhkahdca a dsah dha adlasldoao al ladjjada</description> <left>18</left> </item> </products>

The idea is that a User can reserve numbers of the products by a php page, which will automatically tell him how many are left and change the values of the xml file of the certain products. Here is my php page - metro.php:

[php]<?php
$products = simplexml_load_file(“example.xml”);
$max_per_row = 2;
$item_count = 0;

echo ‘

’;
foreach ($products->item as $item)
{
if ($item_count == $max_per_row)
{
echo “”;
$item_count = 0;
}
echo '', PHP_EOL;
$item_count++;

}
echo ’

Name: 
Product
’ , 'Description: ', $item->description , '
Left: 





'; ?>[/php]

And here is the PHP page for the action of the form. This is metro2.php:

[php]<?php
$products = simplexml_load_file(“example.xml”);
$fname = $_POST[‘fname’];
$left = $_POST[‘left’];
$new_left = $left - 1;
echo 'You want to reserve ', $fname, ‘.
’;
echo 'There are now only ‘, $new_left, ’ of this product.’;
$products->item->left = $new_left;
$products->asXML(“example.xml”);
?>[/php]

The problem is as follows. If we assume that I have 33 Aiwas and 17 SONYs, if I hit Reserve! for an Aiwa it gives me on metro2.php that 32 Aiwas, remain, but CHANGES THE VALUE of the LEFT ITEMS for the SONY, so when I reload the page metro.php it gives me that I have 33 Aiwas and 32 SONYs… It clearly does not change the value of the correct child of the products… Where is my mistake. I guest somewhere in page metro2.php… But i donno what to do… Please help :slight_smile: Thanks in advance!

ok, you had a few problems. please read my fixes(as comments in php). long story short, you are not specifying which element in the simplexml object you want to write to, so it defaults to the first one. below i make it so that each item in the xml has an id and it is passed through your form to your metro2.php

first, your xml, with an id attribute added to each item you have (item id=“0”, item id=“1” etc)
[php]<?xml version="1.0" encoding="ISO-8859-1"?>


Flat Screen Television SONY KDL-4500WEED
http://img.zap.co.il/pics/2/7/9/2/37682972c.gif
This is our newest TV set from the SONY Comp. and we hope you buy it motheOMITTEDerzzzz…zasdhaskjdhkahdca a dsah dha adlasldoao al ladjjada
17


Aiwa ozvuchitelna sistema
http://crev.vo.llnwd.net/o42/audioreview/images/products/product_119021.jpg
This is our newest TV set from the Aiwa Comp. and we hope you buy it motheOMITTEDerzzzz…zasdhaskjdhkahdca a dsah dha adlasldoao al ladjjada
12


Blu-Ray DVD Player Panasonic
http://news.cnet.com/i/bto/20070725/BD-UP5000_overhead.jpg
This is our newest TV set from the Aiwa Comp. and we hope you buy it motheOMITTEDerzzzz…zasdhaskjdhkahdca a dsah dha adlasldoao al ladjjada
33


DURO na SHISH…
http://media.otkrovenia.com/profiles/DureFF.jpg
This is our newest TV set from the Aiwa Comp. and we hope you buy it motheOMITTEDerzzzz…zasdhaskjdhkahdca a dsah dha adlasldoao al ladjjada
18

[/php]

then your initial form page:
[php]
// separate bulk html from php when possible

<?php $products = simplexml_load_file("example.xml"); $max_per_row = 2; $item_count = 0;

foreach ($products->item as $item)
{
if ($item_count == $max_per_row)
{
echo “

”;
$item_count = 0;
}
//below i END php so it just interprets the html, as there is so much html compared to php here. you can see that i individually activate it and echo values instead of echoing all the html.
?> <?php $item_count++; } ?>
Name: 
Product
' , 'Description: <?php echo $item->description ?>
Left: 
<?php // here's the real fix on this page, adding an id hidden input that passes the item's id from the simplexml object ?>

[/php]

and finally your metro2 page:
[php]<?php
$products = simplexml_load_file(“example.xml”);
$fname = $_POST[‘fname’];
// accept the _POST id and convert it to integer if it is a number (other problems if not)
$id = (is_numeric($_POST[‘id’]) ? (int)$_POST[‘id’] : 0);
$left = $_POST[‘left’];
$new_left = $left - 1;
echo 'You want to reserve ', $fname, ‘.
’;
echo 'There are now only ‘, $new_left, ’ of this product.’;
//set new left, this time specifying the item id you want to write to
$products->item[$id]->left = $new_left;
$products->asXML(“example.xml”);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service