我正在使用一個顯示大約20個產品的列表的XML產品列表,並且在每個<item>
中都有一個<description>
,然後各有一個。這裏有一個例子:使用PHP foreach使用SimpleXML解析多個嵌套元素作爲無序列表
我使用的是foreach
循環使用SimpleXML在產品數據拉,所有的個人標記,如<image>
和<title>
工作正常。
我的問題是,它只解析每個產品的第一個<bullet>
,而不是所有的產品。爲了解決這個問題,我試圖添加一個for
循環,該循環定位在<description>
標記中的所有嵌套項目,然後輸出很多內容。 但它現在將XML文件中第一項的第一個<bullet>
給我,並將其放在每個產品下,而不是爲每個相應產品顯示多個項目符號,這正是我想要的。
我會發布我的腳本,希望有人能夠指出我要出錯的地方。
<?php
$items = simplexml_load_file('http://www.itclear.com/BestSellers.xml');
foreach ($items->channel->item as $item):
$title=$item->title;
$image=$item->image;
$price=$item->price;
$description=$item->description;
$link=$item->link;
echo '
<div class="xsmall-12 medium-6 large-4 columns product">
<div class="inner">
<div class="product-image">
<img class="product-image" src="',$image,'"/>
</div>
<h2 class="product-title"><strong>',$title,'</strong></h2>
<ul>';
$bullets = $items->channel->item->description;
for($i=0;$i<=$bullets;$i++){
echo '<li>',$bullets[$i]->bullet,'</li>';
}
echo'
</ul><span class="product-price">£',$price,'</span>
<a class="product-link" href="',$link,'" target="_blank" title="Visit ITC Sales to buy ',$title,'">View Deal <i class="fa fa-angle-right" aria-hidden="true"></i></a>
</div>
</div>';
endforeach;
>
對於任何人閱讀此,這對我完全適用於我,我敢肯定,我嘗試了這一點,但我必須做它不同。謝謝IMSoP – Danny