0

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
    $products = simplexml_load_file("example.xml");
    $max_per_row = 2;
    $item_count = 0;

echo '<table width="100%" border="0" cellspacing="2" cellpadding="2"><tr>';
    foreach ($products->item as $item)
{
    if ($item_count == $max_per_row)
    {
        echo "</tr><tr>";
        $item_count = 0;
    }
    echo '<form name="reg" action="metro2.php" method="post"><td width="50%"><b>Name:</b>&nbsp;<input name="fname" maxlength="256" value=" ', $item->name , '" readonly style="width: 300px; border:0;" /><br><img src="' , $item->image, '" alt="Product" height="200"><br>' , '<b>Description:</b>&nbsp;',  $item->description , '<br><b>Left:</b>&nbsp;<input name="left" maxlength="7" value="' , $item->left ,'" readonly style="width: 70px; border:0;" /><br>
  <label>
  <input type="submit" name="button" id="button" value="Reserve!" />
  </label>
</form><br><hr /></td>', PHP_EOL;

    $item_count++;
}
   echo '</tr>
</table>';
?>

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

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

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 :) Thanks in advance!

1 Answer 1

1

You must select the correct child in metro2.php. The best solution is you add the index opf current child as a hidden input field in metro.php. In metro2.php you can take the the index and select the correct child to set the new value.

EDIT:

I take the code above and add the changes:

metro.php:

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

echo '<table width="100%" border="0" cellspacing="2" cellpadding="2"><tr>';

// Change the type of loop for access the items manually and get the correct index.
for ($index = 0, $count = $products->count(); $index < $count; $index++)
{
    // Get the item by index
    $item = $products->children()[$index];
    if ($item_count == $max_per_row)
    {
        echo "</tr><tr>";
        $item_count = 0;
    }
    echo '<td width="50%">
        <form name="reg" action="metro2.php" method="post">
            <b>Name:    </b>&nbsp;
            <input name="fname" maxlength="256" value=" ', $item->name , '" readonly style="width: 300px; border:0;" /><br>
            <img src="' . $item->image . '" alt="Product" height="200"><br>
            <b>Description:</b>&nbsp;' . $item->description . '<br>
            <b>Left:</b>&nbsp;

            <!-- New hidden field -->
            <input type="hidden" name="index" value="' . $index . '" />

            <input name="left" maxlength="7" value="' . $item->left . '" readonly style="width: 70px; border:0;" /><br>
            <label>
                <input type="submit" name="button" id="button" value="Reserve!" />
            </label>
        </form><br>
        <hr />
    </td>', PHP_EOL;

    $item_count++;
}
echo '</tr>
</table>';
?>

metro2.php:

<?php  
$products = simplexml_load_file("example.xml");

// Get the index of item from post request
$index = intval($_POST['index']);

$fname = $_POST['fname'];

// Parse the left value to in an integer
$left = intval($_POST['left']);
$new_left = $left - 1;
echo 'You want to reserve&nbsp;<b>', $fname, '</b>.<br />';
echo 'There are now only&nbsp;<b>', $new_left, '</b>&nbsp;of this product.';

// Select the correct item which by index and set new value
// Use the children method and not the item attribute for select the item,
$products->children()[$index]->left = $new_left;

$products->asXML("example.xml");
?>

I make also some HTML changes.

EDIT #2:

If you don't understand the foreach statement correctly look here.

EDIT #3:

Changed the type of loop and the selection of item from object $products to get the item by index.

Sign up to request clarification or add additional context in comments.

10 Comments

I was going to answer with this and some example code. But you've explained it pretty well.
does this mean that I have to add IDs to the <item> in the xml file?
Could you help me with some sample code, because I am really new to using XML with PHP and this is all I came with in one day... I have a very basic knowledge of simplexml
You doen't must add IDs to your items, but you can do it. I think the easiest way is to use also the key in your loop: foreach ($products->item as $index => $item) { ... }.
Im not sure I understand... If somebody could help me with some code it would be really nice. I tried foreach ($products->item as $index => $item)item as $index => $item in the code of metro.php, but it still gives the same result...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.