2016-11-03 14 views
0

我正在使用一個顯示大約20個產品的列表的XML產品列表,並且在每個<item>中都有一個<description>,然後各有一個。這裏有一個例子:使用PHP foreach使用SimpleXML解析多個嵌套元素作爲無序列表

XML File with <bullet> List

我使用的是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">&pound;',$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; 

>

Rendered HTML & CSS

回答

1

你的問題是這一行:

$bullets = $items->channel->item->description; 

此行有沒有提到你在外層foreach循環遍歷元素,所以沒有辦法知道你想看哪個item。它只是說「看看文檔中的第一個<channel>,然後看看第一個<item>,然後看看<description>這個元素」。

你需要的是用在循環中定義的$item變量,並期待在<description>元素

$bullets = $item->description; 

但這個變量名是不太正確的 - $bullets變量現在擁有一個或更多<description>元素,而不是其下的<bullet>元素。因此,我們應該將說:

$description = $item->description; 
$bullets = $description->bullet; 

請注意,我們現在不需要$bullets[$i]->bullet,因爲$bullets[$i]應該已經是<bullet>元素;你還需要count()這個子彈,這在你的代碼中是缺少的。因此,我們有:

for($i=0;$i<=count($bullets);$i++){ 
    echo '<li>',$bullets[$i],'</li>'; 
} 

爲了簡化這一切,你可以只使用foreach環的相同的風格,你已經在文件的頂部:

foreach ($item->description->bullet as $bullet) { 
    echo '<li>',$bullet,'</li>'; 
} 
+0

對於任何人閱讀此,這對我完全適用於我,我敢肯定,我嘗試了這一點,但我必須做它不同。謝謝IMSoP – Danny

0

我還發現了一個替代解決方案這對於那些也陷入了類似的問題。這裏的代碼:

<?php 

     $xml = 'http://www.itclear.com/BestSellers.xml'; 

     $items = simplexml_load_file($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 class="product-description">'; 

      foreach ($item->description->bullet as $bullet) { 
       echo '<li>'; 
       $b = $bullet; 
       echo $b; 
       echo "</li>"; 

      } 


      echo' 
       </ul><span class="product-price">&pound;',$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>'; 

     } 

    ?> 
+1

這不是一個「替代」解決方案,它正是我在答案的最後寫的。 (除了在這裏你創建了一個變量'$ b',其值完全與'$ bullet'完全相同,完全沒有理由。) – IMSoP