2012-09-25 22 views
0

這裏是我到目前爲止的標記:顯示子類別中自從

<?php 

$categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id, c.sort_order, c.date_added, c.last_modified from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id order by c.sort_order ASC"); 
while ($categories = tep_db_fetch_array($categories_query)){ 

echo '<tr class="dataTableRow"> 
<td class="dataTableContent">' . $categories ['categories_name'] . '</td>'; 
?> 

<td class="dataTableContent"></td> 

<?php 
echo '</tr>'; 
} 
?> 

這將顯示所有類別和子類別中的一個頁面上,但在沒有訂單,我需要的是對要在主類別下顯示的子類別。我有一些線索,我該怎麼做,sort_order字段和parent_id字段之間的鏈接。主要類別使用sort_order字段作爲id,並且subccat使用parent_id字段對它們所屬的類別進行排序。有任何想法嗎?

回答

2

我不知道該怎麼辦顯示類別樹單MySQL查詢,所以我建議使用遞歸PHP函數:

<?php 
    function showChildCategories($parent) { 
     $parent = intval($parent); 

     // Select only categories with parent_id = $parent 
     $categories_query = tep_db_query("select 
       cd.categories_name, 
       c.categories_id 
      from " . TABLE_CATEGORIES . " c, " . 
       TABLE_CATEGORIES_DESCRIPTION . " cd 
      where c.categories_id = cd.categories_id 
       and c.parent_id = $parent 
      order by c.sort_order ASC"); 

     // Go through all query result rows 
     while ($categories = tep_db_fetch_array($categories_query)){ 
      // Show category name 
      echo '<tr class="dataTableRow"><td class="dataTableContent">' . 
       $categories ['categories_name'] . '</td></tr>'; 

      // Show child categories for current row 
      showChildCategories($categories ['categories_id']); 
     } // while 
    } // function showChildCategories 

    // Show top-level categories and all their children recursively 
    showChildCategories(0); 
?> 
+0

伊夫用你給的建議,這似乎ahve的工作,但我我得到一個錯誤說'致命錯誤:無法重新聲明showChildCategories()(以前在43行中的.. \(文件名:43)聲明任何想法,這是什麼意思? – user180857

+0

它是好的,我得到它修復。 alexkorep – user180857