开发者

PHP SimpleXML repeated data

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>

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">&pound;<?=$item->price?></div>
        <a href="<?=$item->url?>" class="shopNow">SHOP</a>
    </div>

        <? 
        $i++;
        unset($item);
    }
    unset($products);
}

Can anybody help? This where I am so far, unset doesn't work :( <shop> is $xml


You should use $section variable to count it products.

So foreach($xml->section->products as $products){ -> foreach($section->products as $products){

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜