2011-11-11 4 views
0

PHP XML을 사용하여 XML 파일을 읽고 있습니다. I 각 범주 노드를 반복하여 포함 된 범주에 따라 포함하는 노드 데이터를 표시하여 하위 범주를 표시합니다. 어떻게 카테고리에 하위 카테고리가 없는지 감지해야합니다. 필요한 것은 어쩌면 모든 parent_id 노드를 배열로 가져 와서 cat_id가 배열에 있는지 검사하여 종속 항목이 있는지 여부를 확인하는 것입니다.php - simpleXML 동일한 노드를 배열에 넣으십시오.

$cat_xml = simplexml_load_file(XML_PATH.'categories/'. $clientID . '.xml'); 

      //get all the categories and go through each one 
      foreach($cat_xml as $cat){ 
       if (//WHAT GOES HERE??){ 
        //display if cat is top level or has been selected 
        if ($cat->parentID == $cat_parent_id){ 
         //check product isn't set to hidden 
         if ($cat->visible == 1){ 
          echo '<div class="catbox">'; 
           echo "<a href=\"index.php?pageID=$pageID&cat_parent_id=$cat->id&cat_name=$cat->Name\"> $cat->Name </a><br />"; 
          echo '</div>'; 
         } 
        } 
       } else { 
        echo '<p>There are no sub categories.</p>'; 
       } 
      } 

[편집]
는 XML 이하이고, I는 "parentID"의 배열을 얻을 필요

<categories> <category> <id>740073</id> <Name>Leetee Cat 1</Name> <parentID>0</parentID> <Description><![CDATA[Charlotte Balbier Weddinhg <em>dresses</em>,<strong> blah blah blah!!<br /> </strong>]]></Description> <imageURL>alice-charlotte-balbier-english-tea-party-collection-2011.jpg</imageURL> <sequence>0</sequence> <visible>1</visible> </category> 
+1

XPath –

+0

과 같은 소리가 나옵니다. XML 스 니펫을 표시하고 정확하게 구문 분석해야하는 항목을 명확히하십시오. – Gordon

+0

Ive가 원래 질문에 XMl을 추가했습니다. - 감사합니다. – LeeTee

답변

1

XPath은의 상위 ID를 포함하는 카테고리를 검색하는 데 사용될 수있는 값 현재 카테고리.

foreach($cat_xml as $cat) { 
    $cat_id = intval($cat->id); 
    // Find categories that call this one their parent 
    $child_cats = $cat_xml->xpath("/categories/category[parentID/text()={$cat_id}]"); 
    if (!empty($child_cats)){ 
     //display if cat is top level or has been selected 
     if ($cat->parentID == $cat_parent_id){ 
      // .... 
     } 
    } else { 
     echo '<p>There are no sub categories.</p>'; 
    } 
}