dtudj42064 2015-12-12 10:58
浏览 20

如何列出类别和无限深度的类别?

My mysqli class is here. https://github.com/joshcam/PHP-MySQLi-Database-Class

I created categories table like :

enter image description here

Some notes.

If the row cat_parent == 0 it means top level category.

Also, cat_status == 0 that item, must be in the tree.

Now, i want to list them like a category tree ? Or want to use in select box with depth.

Could anyone give me an example please.

NOTE

Also tried this advice http://forums.codewalkers.com/php-coding-7/unlimited-infinite-levels-depths-of-categories-with-php-and-mysql-1087307.html but someone said this is worst coding.

Thank you.

  • 写回答

1条回答 默认 最新

  • dongye3917 2015-12-12 21:03
    关注

    I have already done.

    Here is the result.

    function __getCats($cat_parent=0) {
        global $db;
        $cats = $db->get('categories');
        if ($cats>0) {
            $list_items = array();
            foreach ( $cats as $cat )
            {
                if ( ( int ) $cat['cat_parent'] !== ( int ) $cat_parent )
                {
                    continue;
                }
                $list_items[] = '<li>';
                $list_items[] = '<a href="#' . $cat['id'] . '">';
                $list_items[] = $cat['cat_name'];
                $list_items[] = '</a>';
                $list_items[] = __getCats( $cat['id'] );
                $list_items[] = '</li>';
    
            }
            $list_items = implode( '', $list_items );
            if ( '' == trim( $list_items ) )
            {
                return '';
            }
            return '<ul>' . $list_items . '</ul>';
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)