zoukankan      html  css  js  c++  java
  • 无限极分类

      什么是无限级分类?

      无限级分类是一种分类技巧,例如部门组织,文章分类,学科分类等常用到无限级分类,将其简单理解成分类就好了。其实我们仔细想一下,生活中的分类简直太多了,衣服可以分为男装和女装,也可以分为上衣和裤子,也可以根据年龄段分类。分类无处不在,分类显得“无限”。我这里就不说无限分类的必要性了。

      无限级分类原理简介

      无限分类看似"高大上",实际上原理是非常简单的 。无限分类不仅仅需要代码的巧妙性,也要依托数据库设计的合理性。要满足无限级分类,数据库需要有两个必须的字段,id,pid。id用来标识自身,而pid则是用来表明父级id。也就是说,每个分类记录不仅描述了自身,还描述了与其关心最为紧密的另一个id。看似复杂的事情被这样一个小技巧解决了。

      闲话不多说,该展现本文的实例了。

      作为一个狂热海贼迷,这篇的实例我就以《海贼王》人物组织做案例。

      数据库准备: 

      建表onepiece:

    create table onepiece(
        id int auto_increment,
        pid int not null,
        name varchar(225) not null,
        primary key(id)
    );

       插入测试数据:

    insert onepiece values
        (1,0,'海军'),
        (2,0,'海贼'),
        (3,0,'革命军'),
        (4,1,'青雉'),
        (5,1,'赤犬'),
        (6,1,'黄猿'),
        (7,2,'四皇'),
        (8,2,'七武海'),
        (9,2,'草帽海贼团'),
        (10,9,'索隆'),
        (11,7,'香克斯'),
        (12,8,'多弗朗明哥'),
        (13,8,'克洛克达尔');
    复制代码

      这里还是科普下海贼王里面的设定:世界分为三大阵营:海军,海贼,革命军。海军有大将:青雉,赤犬,黄猿。海贼有:四皇,七武海,草帽海贼团。四皇有香克斯,七武海有多弗朗明哥,克洛克达尔,草帽海贼团有索隆。(打个广告:海贼王真的很好看)。

      最终目的:

      我们今天制作的是两种形式的无限级分类形式,一种是下拉列表式,一种则是导航Link式的。直接上效果图了:

    下拉列表式导航Link式

      实例代码:

      我封装了一个Unlimited类,用来调用diaplayList()展现下拉列表形式,调用diaplayLink展现导航Link分类。也可以增加(addNodes())和删除(deleteNodes)分类。

      

    <?php
    
    class Unlimited{
        protected $mysqli;
        public function __construct($config){
            $this->mysqli=new mysqli($config['host'],$config['user'],$config['pwd']);
            $this->mysqli->select_db($config['db']);
            $this->mysqli->set_charset('utf8');
            if ($this->mysqli->connect_errno) {
                echo $this->mysqli->connect_error;
            }
        }    
    
        private function getList($pid=0,&$result=array(),$spac=0){
            $spac=$spac+2;
            $sql="select * from onepiece where pid={$pid}";
            $rs=$this->mysqli->query($sql);
            while($row=$rs->fetch_assoc()) {
                $row['name']=str_repeat('&nbsp;&nbsp',$spac).$row['name'];
                $result[]=$row;
                $this->getList($row['id'],$result,$spac);            
            }
            return $result;
        }
        /**
         * 展现下拉列表式分类
         * @return [type] 
         */
        public function displayList(){
            $rs=$this->getList();
            $str="<select name='cate'>";
    
            foreach ($rs as $key => $val) {
                $str.="<option >{$val['name']}</option>";
            }
            $str.="</select>";
            return $str;
        }
    
        private function getLink($cid,&$result=array()){
            $sql="select * from onepiece where id={$cid}";
            $rs=$this->mysqli->query($sql);
            if($row=$rs->fetch_assoc()){
                $result[]=$row;
                $this->getLink($row['pid'],$result);
            }
            return array_reverse($result);
        }
        /**
         * 展现导航Link
         * @param  [type] $cid [description]
         * @return [type]      [description]
         */
        public function displayLink($cid){
            $rs=$this->getLink($cid);
            $str='';
            foreach ($rs as $val) {
                $str.="<a href=''>{$val['name']}</a>>";
            }
    
            return $str;
        }
        /**
         * 增加分类
         * @param [type] $pid  父类id
         * @param [type] $name 本类名
         */
        public function addNodes($pid,$name){
            $sql="insert into onepiece values('',{$pid},'".$name."')";
            if($this->mysqli->query($sql)){
    
                return true;
    
            }
        }
        /**
         * 删除分类
         * @param  [type] $id 本类id
         * @return [type]     
         */
        public function deleteNodes($id){
            $sql="select * from onepiece where pid ={$id}";
            $rs=$this->mysqli->query($sql);
            if($row=$rs->fetch_assoc()){
                $mes="还有子元素,请勿删除";
            }else{
                $sql="delete from onepiece where id={$id}";
                if($this->mysqli->query($sql)){
                    $mes="删除成功";
                }
            }
            return $mes;
        }
    }
    复制代码
  • 相关阅读:
    JavaScript和DOM
    CSS补充以及后台页面布局
    HTML标签和CSS基础
    基于SQLAlchemy实现的堡垒机
    PymySQL
    SQLAlchemy
    负数取模
    list
    算法(3)
    python初识(3)
  • 原文地址:https://www.cnblogs.com/liangzia/p/6112833.html
Copyright © 2011-2022 走看看