PHP-MySQL从产品表创建树形视图 [英] PHP - MySQL create a tree view from product table

查看:59
本文介绍了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.

像这样的树

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

我对查询有点困惑. 谁能帮我吗?

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

预先感谢

推荐答案

对于基本显示,您可以使用:

For basic display, you may use:

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

它将为您提供所有的根节点.

It will give you all the root nodes.

当用户单击节点时,他/她/它向服务器(可能是Ajax)提交请求以获取被单击节点的子节点,例如:

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']);

然后:

SELECT * FROM `Table` WHERE `parent_of` = {$node};

这篇关于PHP-MySQL从产品表创建树形视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆