dongzhi7641 2012-09-25 12:42
浏览 16
已采纳

在oscommerce中的类别下显示子类别

Here is the markup of what I have so far:

<?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>';
}
?>

This displays all the categories and subcategories all on a page, but in no order, what i need is for the subcategories to be displayed under the main category. I have some clue as to how i would do it, a link between the sort_order field and the parent_id field. The main categories use the sort_order field as id, and the subccat's use the parent_id field to sort the category they belong to. Any ideas?

  • 写回答

1条回答 默认 最新

  • duanshan3331 2012-09-25 14:40
    关注

    I'm not sure how to do display category tree with single MySQL query, so I'm suggesting to use recursive PHP function:

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?