Home  >  Article  >  Backend Development  >  thinkphp 无线级别归类

thinkphp 无线级别归类

WBOY
WBOYOriginal
2016-06-13 12:35:10794browse

thinkphp 无线级别分类


name          id    parent_id   path

 新闻          1        0         0

 中国新闻      2        1         0-1

   河北新闻    3        2         0-1-2

      保定新闻                     0-1-2-3

 美国新闻      4        1         0-1

   纽约新闻    5        4         0-1-4

      华尔街新闻6       5         0-1-4-5

 俄罗斯新闻    7        1         0-1

 

数据库字段

mysql> select * from news;

+---------+-----------+------+---------+

| news_id | news_name | pid  | path    |

+---------+-----------+------+---------+

|       1 | 新闻      |    0 | 0       |

|       2 | 中国新闻  |    1 | 0-1     |

|       3 | 美国新闻  |    1 | 0-1     |

|       4 | 河北新闻  |    2 | 0-1-2   |

|       5 | 纽约新闻  |    3 | 0-1-3   |

|       6 | 邯郸新闻  |    4 | 0-1-2-4 |

+---------+-----------+------+---------+

6 rows in set (0.00 sec)

以无线级别的思想查看数据库

mysql> select news_id,news_name,pid,path,concat(path,'-',news_id) as bpath from

news order by bpath;

+---------+-----------+------+---------+-----------+

| news_id | news_name | pid  | path    | bpath     |

+---------+-----------+------+---------+-----------+

|       1 | 新闻      |    0 | 0       | 0-1       |

|       2 | 中国新闻  |    1 | 0-1     | 0-1-2     |

|       4 | 河北新闻  |    2 | 0-1-2   | 0-1-2-4   |

|       6 | 邯郸新闻  |    4 | 0-1-2-4 | 0-1-2-4-6 |

|       3 | 美国新闻  |    1 | 0-1     | 0-1-3     |

|       5 | 纽约新闻  |    3 | 0-1-3   | 0-1-3-5   |

+---------+-----------+------+---------+-----------+

6 rows in set (0.04 sec)

News_id 自增

Pid :当前的pid是,父级news_id,

 

Path:当前的path是父级的path连上父级的news_id

bPath:当前的bpath是当前的path字段加上当前的news_id字段,他是一个临时的字段,这样就可以排序bpath,就可以达到一级一级的浏览的效果

concat()函数的参数是字符串列表,返回结果是 链接参数产生的字符串

 

 

在TP框架中

CategoryAction.class.php

class CategoryAction extends Action{
 public function shows(){
header('Content-Type:text/html;charset=utf8');
  $model=D('News');
  $list=$model->field("news_id,news_name,pid,path,concat(path,'-',news_id) as bpath")->order('bpath')->select();//bPath字段是path字段加上news_id字段
  //var_dump($list);
  /*php中的foreach,添加count子段,在bpath字段中,新闻是2个数字(0,1),中国新闻是3个{0,1,2},美国新闻是3个{0,1,3},河北新闻是4个数字(0,1,2,4)...所以要把-横杠去掉计算bpath的个数,用explode把bpath的字符串分割成数组,在计算数字的长度,值就是count字段的内容,缩进的字符数*/
  foreach($list as $key=>$value){
   //没循环一次就增加一个count字段,值是由字符串分割数组的个数
   $list[$key]['count']=count(explode('-',$value['bpath']));//$list[$key]['count']添加一个临时count字段
   }
  $this->assign('list',$list);
  $this->display();
  }
 
 public function add(){
  $model=D('News');
  $data=array(
  'news_name'=>$_POST['uname'],
  'pid'=>$_POST['pid'],//新添加的父id应该是点击当前option的id,也就是value="{$vo.news_id}",获得表单

 


?>

模板中show.html







Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn