zoukankan      html  css  js  c++  java
  • php不使用递归实现无限极分类

    无限极分类常用的是递归,但是比较不好理解,其实可以用数据库path,pid两个字段的设计来实现无限分类的功能

    1、数据库设计

    通过上图可以看出pid就是该栏目的父id,而path = 父path+pid;

    2、php实现《树形查询》《逆向查询》以及给定栏目id《查询下一级》的方法:

    $mysql = new MySQLi('localhost','root','','test');
    $mysql->query("set names gb2312");
    /**
     * 获取无限分类列表(树形查询) 主要用于后台的栏目列表
     */
    $sql = 'select ac_name,concat(path,"-",ac_id) as bpath from article_class order by bpath,ac_id';
    $result = $mysql->query($sql);
    while($row = $result->fetch_assoc()){
        echo $row['ac_name'].'<br />';
    }
    echo '<hr />';
    /**
     * 指定分类获取全部的父类(逆向查询) 主要用于编辑文章时获取该文章的相关分类
     */
    $ac_id = 10;
    $sqlonly = 'select * from article_class where ac_id='.$ac_id;
    $resultonly = $mysql->query($sqlonly);
    $pclass = array();
    $curclass = '';
    while ($rowonly = $resultonly->fetch_assoc()){
        $curclass = $rowonly['ac_name'];
        $arrpaht = explode('-',$rowonly['path']);
        $arrpaht = implode(',',$arrpaht);
        
        $sqlall = 'select * from article_class where ac_id in ('.$arrpaht.') order by ac_id';
    
        $resultall = $mysql->query($sqlall);
        while($rowall = $resultall->fetch_assoc()){
            $pclass[] = $rowall['ac_name'];    
        }
    }
    array_push($pclass,$curclass);
    echo '<pre>';
        print_r($pclass);
    echo '</pre>';
    echo '<hr />';
    /**
     * 指定分类获取下一级分类 主要用于展开分类时显示下一级分类使用
     */
    function getNextClass($ac_id){
        global $mysql;
        $sql = 'select * from article_class where pid='.$ac_id;
        $resultnext = $mysql->query($sql);
        while($rownext = $resultnext->fetch_assoc()){
            echo $rownext['ac_name'].'<br />';    
        }
    }
    getNextClass(1);

    3、相应的页面效果如下:

    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    html+php超大视频切片上传
    html+php超大视频分片上传
    html+php超大视频上传前端
    html+php超大视频上传讨论
    html+php超大视频上传分享
    MATLAB的设置视点函数view
    matlab 绘制三维图并标注每个点的坐标
    Spring Boot 五种热部署方式,极速开发就是生产力!
    如何提高服务器的并发处理能力?硬核!
    WEB攻击手段及防御第3篇-CSRF
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5175545.html
Copyright © 2011-2022 走看看