Thinkphp无限级分类代码

2025-05-29 0 80

本篇就一点一点教大家写一个无限级分类出来,其实掌握一个知识,最主要的是要掌握无限级分类的逻辑,那么写起来就很容易的多了。
首先看数据库表:xp_cate

Thinkphp无限级分类代码

控制器:CateAction.class.php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
<?php

class CateAction extends Action{

function index(){

$cate=M('Cate');

$list=$cate->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();

foreach($list as $key=>$value){

$list[$key]['count']=count(explode('-',$value['bpath']));

}

$this->assign('alist',$list);

$this->display();

}//添加栏目

function add(){

$cate=new CateModel();if($vo=$cate->create()){

if($cate->add()){

$this->success('添加栏目成功');

}else{

$this->error('添加栏目失败');

}

}else{

$this->error($cate->getError());

}

}}

?>

模型:CateModel.class.php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
<?php

class CateModel extends Model{//对应数据库中的表xp_cate

protected $_auto=array(

array('path','tclm',3,'callback'),

);function tclm(){

$pid=isset($_POST['pid'])?(int)$_POST['pid']:0;

echo ($pid);

if($pid==0){

$data=0;

}else{

$list=$this->where("id=$pid")->find();

$data=$list['path'].'-'.$list['id'];//子类的path为父类的path加上父类的id

}

return $data;

}

}

?>

模板:index.html

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
<form action="/Article/add" method="post">

请选择父级栏目:<select name="pid" size="20">

<option value="0">根栏目</option>

<volist name="alist" id="vo">

<option value="{$vo['id']}">

<for start="0" end="$vo['count']">

&nbsp;&nbsp;

</for>

{$vo['name']}

</option>

</volist>

</select><br />

新的栏目名称:<input type="text" name="name" /><br />

<input type="submit" value="添加栏目" />

</form>

显示结果如下:

Thinkphp无限级分类代码

有没有掌握无限级分类的逻辑,上文分享的thinkphp无限级分类代码,希望对大家的学习有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Thinkphp无限级分类代码 https://www.kuaiidc.com/100943.html

相关文章

发表评论
暂无评论