Home  >  Article  >  php教程  >  php+mysql非递归方法实现无限级分类

php+mysql非递归方法实现无限级分类

WBOY
WBOYOriginal
2016-06-06 19:52:021047browse

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 用php实现无限级的分类,用递归方法是最容易想到的,但递归很占用系统资源,所以一般情况不考虑使用递归, 本文通过非递归数据库的设计的来实现无限级分类 数据库结构如下: id 编号 fid 父分类编号 class

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

    用php实现无限级的分类,用递归方法是最容易想到的,但递归很占用系统资源,所以一般情况不考虑使用递归,

    本文通过非递归数据库的设计的来实现无限级分类

    数据库结构如下:

    id 编号

    fid 父分类编号

    class_name 分类名

    path 分类路径,以 id 为节点,组成类似 ,1,2,3,4, 这样的字符串

    可以假设有如下的数据

    id fid class_name path

    ----------------------------------------------------

    1 0 分类1 ,1,

    2 0 分类2 ,2,

    3 1 分类1-1 ,1,3,

    4 1 分类1-2 ,1,4,

    5 2 分类2-1 ,2,5,

    6 4 分类1-2-1 ,1,4,6,

    ----------------------------------------------------

    PHP代码:

   

    $sql=“SELECT * FROM tree order by path”;

    $result=$nbs->Query($sql);

    while($rows=$nbs->fetch_array($result)){

    if(substr_count($rows['path'],',')>2){

    for($i=0;$i

    echo ' ';

    }

    echo $rows['class_name'].'
';

    }

    ?>

    其中$nbs是数据库操作类.

php+mysql非递归方法实现无限级分类

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn