dongre6270 2013-12-05 12:53
浏览 34
已采纳

使用PHP更改XML文件的值 - 某个子项

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条回答 默认 最新

  • duanemei2194 2013-12-05 13:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭