PHP无限分类实例程序
无限分类的原理:就像windows下新建一个文件夹,在新建的文件夹下又可以新建一个文件夹,这样无限循环下去,无限分类也是这样,父类可以分出它子类,子类又可以分出它的子类,这样一直无限循环下去.
例1,代码如下:
- $yArr = array(
- 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
- 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
- 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
- 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
- 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
- 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
- 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二'),
- 8 => array('id'=>'8','parentid'=>2,'name'=>'二级栏目三'),
- );
-
-
-
-
-
-
-
- function genCate($data, $pid = 0, $level = 0)
- {
- if($level == 10) break;
- $l = str_repeat(" ", $level);
- $l = $l.'└';
- static $arrcat = array();
- $arrcat = emptyempty($level) ? array() : $arrcat;
- foreach($data as $k => $row)
- {
-
-
-
- if($row['parentid'] == $pid)
- {
-
- $row['name'] = $l.$row['name'];
- $row['level'] = $level;
- $arrcat[] = $row;
-
- genCate($data, $psiff, $row['id'], $level+1);
- }
- }
- return $arrcat;
- }
-
- $carr = genCate($yArr);
- echo "<select>";
- foreach($carr as $row)
- {
- echo "<option value={$row['id']}>";
- echo $row['name'];
- echo "</option>";
- }
- echo "</select>";
注:因为是无限次的调用,所以我加了个判断,在层级$level=10的时候让他跳出,没有哪个正常网站会放超过10层的目录结构吧.
执行到static变量后,判断下当前层级,如果层级为0,那么表示这是最高级菜单,需要清空$arrcate的数据重新声明.
例2,代码如下:
- //我们建一个表"class"
- CREATE TABLE `class` (
- `id` int(11) NOT NULL auto_increment COMMENT '分类id',
- `f_id` int(11) NOT NULL COMMENT '父id',
- `name` varchar(25) collate gbk_bin NOT NULL COMMENT '分类名称',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=gbk COLLATE=gbk_bin AUTO_INCREMENT=1 ;
代码如下:
- <?php
-
- header("Content-type:text/html;charset=utf-8");
-
- $db=new mysqli("localhost","root","","news_php100") ;
-
- 或者用mysql_connect这个方式连接。
-
- if(mysqli_connect_errno()){
-
- echo "链接失败:".mysqli_connect_error();
-
- exit(); }
-
- $db->query("set names utf8");
-
- $result=$db->query("select name from class where f_id=0");
-
-
- while($row=$result->fetch_assoc()){
-
- echo $row['name']."< br>";
-
- }
-
-
-
- $result=$db->query("select * from class where f_id=1");
-
-
- while($row=$result->fetch_assoc()){
-
- echo $row['name']."
-
- ";
-
- }
-
-
-
- 10个循环把它每个子类循环出来?如果是更多级分类呢,这样写显然是不现实的。
-
- 不断循环每一个f_id的值,也就是说把每一个f_id值的子类循环出来。
-
-
-
- $result=$db->query("select * from class");
-
- while($row=$result->fetch_assoc()){
-
- $arr[]=array($row[id],$row[f_id],$row[name]);
- 分类的id,f_id,name的信息。
-
- }
-
- function fenlei($f_id=0){
-
- global $arr;
-
- for($i=0;$i< count($arr);$i++){
-
- if($arr[$i][1]==$f_id){
- 开始$f_id=0,也就是把f_id=0的分类输出来。
-
- echo $arr[$i][2]."< br>";
-
- fenlei($arr[$i][0]);
- ,也就是把自己的id作为f_id参数把自己的子类再循环出来。
-
- }
-
- }
-
- }
-
- fenlei();
- ?>
例3,php无限分类,支持输出树状图,代码如下:
- <?php
-
-
-
- class tree
- {
-
-
-
-
- var $arr = array();
-
-
-
-
-
- var $icon = array('│','├','└');
-
-
-
-
- var $ret = '';
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function tree($arr=array())
- {
- $this->arr = $arr;
- $this->ret = '';
- return is_array($arr);
- }
-
-
-
-
-
-
- function get_parent($myid)
- {
- $newarr = array();
- if(!isset($this->arr[$myid])) return false;
- $pid = $this->arr[$myid]['parentid'];
- $pid = $this->arr[$pid]['parentid'];
- if(is_array($this->arr))
- {
- foreach($this->arr as $id => $a)
- {
- if($a['parentid'] == $pid) $newarr[$id] = $a;
- }
- }
- return $newarr;
- }
-
-
-
-
-
-
- function get_child($myid)
- {
- $a = $newarr = array();
- if(is_array($this->arr))
- {
- foreach($this->arr as $id => $a)
- {
- if($a['parentid'] == $myid) $newarr[$id] = $a;
- }
- }
- return $newarr ? $newarr : false;
- }
-
-
-
-
-
-
- function get_pos($myid,&$newarr)
- {
- $a = array();
- if(!isset($this->arr[$myid])) return false;
- $newarr[] = $this->arr[$myid];
- $pid = $this->arr[$myid]['parentid'];
- if(isset($this->arr[$pid]))
- {
- $this->get_pos($pid,$newarr);
- }
- if(is_array($newarr))
- {
- krsort($newarr);
- foreach($newarr as $v)
- {
- $a[$v['id']] = $v;
- }
- }
- return $a;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '')
- {
- $number=1;
- $child = $this->get_child($myid);
- if(is_array($child))
- {
- $total = count($child);
- foreach($child as $id=>$a)
- {
- $j=$k='';
- if($number==$total)
- {
- $j .= $this->icon[2];
- }
- else
- {
- $j .= $this->icon[1];
- $k = $adds ? $this->icon[0] : '';
- }
- $spacer = $adds ? $adds.$j : '';
- $selected = $id==$sid ? 'selected' : '';
- @extract($a);
- $parentid == 0 && $str_group ? eval("$nstr = "$str_group";") : eval
-
- ("$nstr = "$str";");
- $this->ret .= $nstr;
- $this->get_tree($id, $str, $sid, $adds.$k.' ',$str_group);
- $number++;
- }
- }
- return $this->ret;
- }
-
-
-
- function get_tree_multi($myid, $str, $sid = 0, $adds = '')
- {
- $number=1;
- $child = $this->get_child($myid);
- if(is_array($child))
- {
- $total = count($child);
- foreach($child as $id=>$a)
- {
- $j=$k='';
- if($number==$total)
- {
- $j .= $this->icon[2];
- }
- else
- {
- $j .= $this->icon[1];
- $k = $adds ? $this->icon[0] : '';
- }
- $spacer = $adds ? $adds.$j : '';
-
- $selected = $this->have($sid,$id) ? 'selected' : '';
-
- @extract($a);
- eval("$nstr = "$str";");
- $this->ret .= $nstr;
- $this->get_tree_multi($id, $str, $sid, $adds.$k.' ');
- $number++;
- }
- }
- return $this->ret;
- }
-
- function have($list,$item){
- return(strpos(',,'.$list.',',','.$item.','));
- }
- }
- ?>
注:无平台限制,只需要告知id,parentid,name 即可,上面总结了三种无限分类代码都没有平台限制,不过只能使用在php中,我们只要搞清楚id,parentid,name三者的关系即可.
|