无限级分类原理简介

  无限分类看似"高大上",实际上原理是非常简单的 。无限分类不仅仅需要代码的巧妙性,也要依托数据库设计的合理性。要满足无限级分类,数据库需要有两个必须的字段,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;
}
}

php递归无限极分类实例的更多相关文章

  1. asp.net sql无限极分类实例程序

    数据库结构  代码如下 复制代码 create table category(    id                  int,                    clsno         ...

  2. Think PHP递归重新排序无限极子分类数组(递归无限极分类)

    Think PHP递归重新排序无限极子分类数组 // 递归重新排序无限极子分类数组 function recursive($array,$pid=0,$level=0){ $arr = array() ...

  3. php递归无限极分类

    递归无限级分类有几种形式,我这里仅仅举例比較经常使用的三种: 第一种:返回有排序的数组: <?php $data = array( 1 => array( 'id' => 1, 'p ...

  4. php无限极分类递归与普通

    1. 递归 public function getInfo(){$data=$this->select();$arr=$this->noLimit($data,$f_id=0,$level ...

  5. php不使用递归实现无限极分类

    无限极分类常用的是递归,但是比较不好理解,其实可以用数据库path,pid两个字段的设计来实现无限分类的功能 1.数据库设计 通过上图可以看出pid就是该栏目的父id,而path = 父path+pi ...

  6. PHP实现无限极分类的两种方式,递归和引用

    面试的时候被问到无限极分类的设计和实现,比较常见的做法是在建表的时候,增加一个PID字段用来区别自己所属的分类 $array = array( array('id' => 1, 'pid' =& ...

  7. php无限极分类以及递归(thinkphp)

    php无限极分类: 无限极分类重点在于表的设计: 1在model中: class CatModel extends Model{ protected $cat = array(); public fu ...

  8. 【laravel5.4】Baum无限极分类和collect助手函数、transform()中间件(转换数据)方法使用

    1.目的,无限极分类 /* * getdepartment:获取[当前登录用户对应公司的所有有效部门] * DB::table ==>返回查询构造器结果,不会返回一个collect实例 * 而 ...

  9. js实现无限极分类

    转载注明出处!!! 转载注明出处!!! 转载注明出处!!! 因为要实现部门通讯录,后台传来的数据是直接从数据库里拿的部门表,所以没有层级分类,只有parentId表示从属关系,所以分类的事情就交给我来 ...

随机推荐

  1. XML 参考:XML基础 XML 简介

    XML 参考:XML基础 -- XML简介和用途 转:http://www.cnblogs.com/Dlonghow/archive/2009/01/22/1379799.html XML 参考:XM ...

  2. win7/IE8无法加载QCbin的插件

    pian A: 1.控制面板->系统和安全->更改用户账户控制设置->安全等级调至最低->关机重启 2.打开IE浏览器->工具->Internet选项->高级 ...

  3. [LintCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  4. ubuntu 安装 wkhtmltopdf 的方法

    参考自:http://vivianyw.blog.163.com/blog/static/1345474222014334256367/ wkhtmltopdf有编译好的Linux版本,找到http: ...

  5. 数的长度---nyoj69

    超时 #include <stdio.h>#include <string.h>#define M 1000001int shu[M]; int main(){ int n, ...

  6. 获取 苹果UDID 序列号

    UDID是什么? UDID 是由子母和数字组成的40个字符串的序号,用来区别每一个唯一的iOS设备,包括 iPhones, iPads, 以及 iPod touches,这些编码看起来是随机的,实际上 ...

  7. IOS第九天(1:QQ聊天界面frame模型)

    ///  控制层 #import "HMViewController.h" #import "HMMessageModel.h" #import "H ...

  8. 本地化word复制来的网页中的图片

    复制一个网页到word文档中,图片会以链接到网页里图片,而不是本地化保存在文档里.为了让图片存在文档里,而不是每次链接到外部,可以这样做. 全选文档,菜单栏里的编辑,点击链接,断开所有链接. 然后再复 ...

  9. .htaccess 重写去index.php

    .htaccess 重写去index.php <IfModule mod_rewrite.c>RewriteEngine onRewriteCond %{REQUEST_FILENAME} ...

  10. JS-页面操作

    --刷新页面 window.location.reload();