Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP更改XML文件的值-特定子文件_Php_Xml_Simplexml - Fatal编程技术网

使用PHP更改XML文件的值-特定子文件

使用PHP更改XML文件的值-特定子文件,php,xml,simplexml,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/

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

索尼KDL-4500平板电视
http://img.zap.co.il/pics/2/7/9/2/37682972c.gif
这是我们索尼公司的最新电视机。我们希望你能买它…一种dha adlasldoao al ladjjada
17
Aiwa ozvuchitelna sistema
http://crev.vo.llnwd.net/o42/audioreview/images/products/product_119021.jpg
这是我们爱华公司的最新电视机。我们希望你能买它…一种dha adlasldoao al ladjjada
12
松下蓝光DVD播放机
http://news.cnet.com/i/bto/20070725/BD-UP5000_overhead.jpg
这是我们爱华公司的最新电视机。我们希望你能买它…一种dha adlasldoao al ladjjada
33
杜罗娜什。。。
http://media.otkrovenia.com/profiles/DureFF.jpg
这是我们爱华公司的最新电视机。我们希望你能买它…一种dha adlasldoao al ladjjada
18
其思想是,用户可以通过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
$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中选择正确的子项。最好的解决方案是将索引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>';
    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
$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>';
?>

我将用这个和一些示例代码来回答。但是你已经解释得很好了。这是否意味着我必须在xml文件中添加ID?你能帮我一些示例代码吗,因为我对在PHP中使用xml非常陌生,这就是我在一天内得到的全部。。。我对simplexml有一个非常基本的了解,您不必为项目添加ID,但您可以这样做。我认为最简单的方法是使用循环中的键:
foreach($products->item as$index=>$item){…}
。。。如果有人能帮我写些代码,那就太好了。我在metro.php的代码中尝试了foreach($products->item as$index=>$item)item as$index=>$item,但它仍然给出了相同的结果。。。