dsdtumf776629385 2013-06-03 06:48
浏览 50
已采纳

PHP - MySQL从产品表创建树视图

I have a category table as follows. Which I am saving category, subcategories in that table ( Multiple sub-categories ). My table looks like

id     |    name       |  parent_of    |   created_on
-------+---------------+---------------+---------------------
1      |   Name 1      |  0            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
2      |   Name 2      |  0            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
3      |   Name 3      |  1            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
4      |   Name 4      |  1            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
5      |   Name 5      |  3            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------
6      |   Name 6      |  3            |  2013-05-1 00:00:00
-------+---------------+---------------+---------------------

Now I need to query this table and create a tree structure as follows to make a easy navigation through categories in front end.

A tree like follows

1
  |--> 3
  |   |--> 5
  |   |--> 6
  |--> 4
2

I am bit confused about the query. Can anyone please help me ?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongliping003116 2013-06-03 06:55
    关注

    For basic display, you may use:

    SELECT * FROM `Table` WHERE `parent_of` = 0;
    

    It will give you all the root nodes.


    When user click on a node, he / she / it submits request to the server (Ajax maybe) to fetch child nodes for clicked one, like:

    $node = abs((int)$_GET['node']);
    

    then:

    SELECT * FROM `Table` WHERE `parent_of` = {$node};
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题