PHP SimpleXML重复数据 [英] PHP SimpleXML repeated data
问题描述
使用SimpleXML从基本XML文件中提取数据.XML看起来像:
Using SimpleXML to pull data from a basic XML file. The XML looks something like:
<shop>
<section>
<title></title>
<products>
<item>
...
</item>
</products>
</section>
</shop>
我可以加载和循环出节点,但是当我用每个小节项目填充HTML时,它会继续重复第一个小节项目.我很困惑,因为每个部分的名称都正确输出,但是使用的是第一组项目.
I can load and loop out the nodes, but when I populate the HTML with each sections items it keeps repeating the first sections items. I'm confused because each section name is outputting correctly but using the first set of items.
foreach($xml->section as $section){
$i=0;
echo '<div class="section"><div class="sectionTop"><h3>'.$section->title.'</h3><a class="expand">+/-</a></div>';
foreach($xml->section->products as $products){
foreach($products->item as $item){
if($i==3){echo '</div><div class="extra">';}?>
<div class="item">
<img src="<?=$item->image?>" />
<div class="prodName"><?=$item->name?></div>
<div class="price">£<?=$item->price?></div>
<a href="<?=$item->url?>" class="shopNow">SHOP</a>
</div>
<?
$i++;
unset($item);
}
unset($products);
}
有人可以帮忙吗?这是我到目前为止无法设置的地方:( << shop>
是 $ xml
Can anybody help? This where I am so far, unset doesn't work :( <shop>
is $xml
推荐答案
您应该使用$ section变量来计算其乘积.
You should use $section variable to count it products.
所以foreach($ xml-> section-> products as $ products){->foreach($ section-> products as $ products){
So foreach($xml->section->products as $products){ -> foreach($section->products as $products){
这篇关于PHP SimpleXML重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!