Rumah  >  Artikel  >  php教程  >  php利用递归函数实现无限级分类,php递归

php利用递归函数实现无限级分类,php递归

WBOY
WBOYasal
2016-06-13 08:56:221081semak imbas

php利用递归函数实现无限级分类,php递归

  相信很多学php的很多小伙伴都会尝试做一个网上商城作为提升自己技术的一种途径。各种对商品分类,商品名之类的操作应该是得心应手,那么就可以尝试下无限级分类列表的制作了。

  什么是无限级分类?

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

  无限级分类原理简介

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

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

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

  数据库准备: 

  建表onepiece:

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

   插入测试数据:

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

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

  最终目的:

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

下拉列表式导航Link式

  实例代码:

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

  

<?<span>php

</span><span>class</span><span> Unlimited{
    </span><span>protected</span> <span>$mysqli</span><span>;
    </span><span>public</span> <span>function</span> __construct(<span>$config</span><span>){
        </span><span>$this</span>->mysqli=<span>new</span> mysqli(<span>$config</span>['host'],<span>$config</span>['user'],<span>$config</span>['pwd'<span>]);
        </span><span>$this</span>->mysqli->select_db(<span>$config</span>['db'<span>]);
        </span><span>$this</span>->mysqli->set_charset('utf8'<span>);
        </span><span>if</span> (<span>$this</span>->mysqli-><span>connect_errno) {
            </span><span>echo</span> <span>$this</span>->mysqli-><span>connect_error;
        }
    }    

    </span><span>private</span> <span>function</span> getList(<span>$pid</span>=0,&<span>$result</span>=<span>array</span>(),<span>$spac</span>=0<span>){
        </span><span>$spac</span>=<span>$spac</span>+2<span>;
        </span><span>$sql</span>="select * from onepiece where pid={<span>$pid</span>}"<span>;
        </span><span>$rs</span>=<span>$this</span>->mysqli->query(<span>$sql</span><span>);
        </span><span>while</span>(<span>$row</span>=<span>$rs</span>-><span>fetch_assoc()) {
            </span><span>$row</span>['name']=<span>str_repeat</span>(' &nbsp',<span>$spac</span>).<span>$row</span>['name'<span>];
            </span><span>$result</span>[]=<span>$row</span><span>;
            </span><span>$this</span>->getList(<span>$row</span>['id'],<span>$result</span>,<span>$spac</span><span>);            
        }
        </span><span>return</span> <span>$result</span><span>;
    }
    </span><span>/*</span><span>*
     * 展现下拉列表式分类
     * @return [type] 
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> displayList(){
        </span><span>$rs</span>=<span>$this</span>-><span>getList();
        </span><span>$str</span>="<select name='cate'>"<span>;

        </span><span>foreach</span> (<span>$rs</span> <span>as</span> <span>$key</span> => <span>$val</span><span>) {
            </span><span>$str</span>.="<option >{<span>$val</span>['name']}</option>"<span>;
        }
        </span><span>$str</span>.="</select>"<span>;
        </span><span>return</span> <span>$str</span><span>;
    }

    </span><span>private</span> <span>function</span> getLink(<span>$cid</span>,&<span>$result</span>=<span>array</span><span>()){
        </span><span>$sql</span>="select * from onepiece where id={<span>$cid</span>}"<span>;
        </span><span>$rs</span>=<span>$this</span>->mysqli->query(<span>$sql</span><span>);
        </span><span>if</span>(<span>$row</span>=<span>$rs</span>-><span>fetch_assoc()){
            </span><span>$result</span>[]=<span>$row</span><span>;
            </span><span>$this</span>->getLink(<span>$row</span>['pid'],<span>$result</span><span>);
        }
        </span><span>return</span> <span>array_reverse</span>(<span>$result</span><span>);
    }
    </span><span>/*</span><span>*
     * 展现导航Link
     * @param  [type] $cid [description]
     * @return [type]      [description]
     </span><span>*/</span>
    <span>public</span> <span>function</span> displayLink(<span>$cid</span><span>){
        </span><span>$rs</span>=<span>$this</span>->getLink(<span>$cid</span><span>);
        </span><span>$str</span>=''<span>;
        </span><span>foreach</span> (<span>$rs</span> <span>as</span> <span>$val</span><span>) {
            </span><span>$str</span>.="<a href=''>{<span>$val</span>['name']}</a>>"<span>;
        }

        </span><span>return</span> <span>$str</span><span>;
    }
    </span><span>/*</span><span>*
     * 增加分类
     * @param [type] $pid  父类id
     * @param [type] $name 本类名
     </span><span>*/</span>
    <span>public</span> <span>function</span> addNodes(<span>$pid</span>,<span>$name</span><span>){
        </span><span>$sql</span>="insert into onepiece values('',{<span>$pid</span>},'".<span>$name</span>."')"<span>;
        </span><span>if</span>(<span>$this</span>->mysqli->query(<span>$sql</span><span>)){

            </span><span>return</span> <span>true</span><span>;

        }
    }
    </span><span>/*</span><span>*
     * 删除分类
     * @param  [type] $id 本类id
     * @return [type]     
     </span><span>*/</span>
    <span>public</span> <span>function</span> deleteNodes(<span>$id</span><span>){
        </span><span>$sql</span>="select * from onepiece where pid ={<span>$id</span>}"<span>;
        </span><span>$rs</span>=<span>$this</span>->mysqli->query(<span>$sql</span><span>);
        </span><span>if</span>(<span>$row</span>=<span>$rs</span>-><span>fetch_assoc()){
            </span><span>$mes</span>="还有子元素,请勿删除"<span>;
        }</span><span>else</span><span>{
            </span><span>$sql</span>="delete from onepiece where id={<span>$id</span>}"<span>;
            </span><span>if</span>(<span>$this</span>->mysqli->query(<span>$sql</span><span>)){
                </span><span>$mes</span>="删除成功"<span>;
            }
        }
        </span><span>return</span> <span>$mes</span><span>;
    }
}</span>

  类中函数主要采取了递归函数的方法,如果理解深刻理解递归函数,其余的部分也就水到渠成了。后面的php递归函数的三种方法详细介绍了php递归函数的原理。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn