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

时间:2013-12-05 12:53:46

标签: php xml simplexml

大家好!我的问题如下: 我正在使用带有产品信息的xml文件。这是 - 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>

这个想法是用户可以通过php页面保留产品的数量,这将自动告诉他剩下多少产品并更改某些产品的xml文件的值。这是我的php页面 - 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>';
?>

这是表单操作的PHP页面。这是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");
?>

问题如下。如果我们假设我有33个Aiwas和17个SONY,如果我击中Reserve!对于一个爱华,它给了我在metro2.php 32 Aiwas,保持,但改变了索尼左侧项目的价值,所以当我重新加载页面metro.php它给了我,我有33个Aiwas和32个索尼..它显然没有改变产品正确孩子的价值......我的错误在哪里。我在网站metro2.php的某个地方做客...但我不知道该做什么...请帮助:)提前致谢!

1 个答案:

答案 0 :(得分:1)

您必须在metro2.php中选择正确的孩子。最好的解决方案是将index opf current child添加为metro.php中的隐藏输入字段。在metro2.php中,您可以获取索引并选择正确的子项来设置新值。

修改

我接受上面的代码并添加更改:

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");
?>

我还做了一些HTML更改。

编辑#2:

如果您不理解foreach语句,请正确查看here

编辑#3:

更改了循环的类型和对象$products中的项目选择,以便按索引获取项目。