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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器