Thinkphp無限級分類代碼
本篇就一點(diǎn)一點(diǎn)教大家寫一個無限級分類出來,其實(shí)掌握一個知識,最主要的是要掌握無限級分類的邏輯,那么寫起來就很容易的多了。
首先看數(shù)據(jù)庫表:xp_cate

控制器:CateAction.class.php
<?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
<?php
class CateModel extends Model{//對應(yīng)數(shù)據(jù)庫中的表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
<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']">
</for>
{$vo['name']}
</option>
</volist>
</select><br />
新的欄目名稱:<input type="text" name="name" /><br />
<input type="submit" value="添加欄目" />
</form>
顯示結(jié)果如下:

有沒有掌握無限級分類的邏輯,上文分享的thinkphp無限級分類代碼,希望對大家的學(xué)習(xí)有所幫助。
- thinkphp5實(shí)現(xiàn)無限級分類
- 使用ThinkPHP的自動完成實(shí)現(xiàn)無限級分類實(shí)例詳解
- ThinkPHP無限級分類原理實(shí)現(xiàn)留言與回復(fù)功能實(shí)例
- ThinkPHP自動填充實(shí)現(xiàn)無限級分類的方法
- thinkphp框架無限級欄目的排序功能實(shí)現(xiàn)方法示例
- thinkPHP實(shí)現(xiàn)遞歸循環(huán)欄目并按照樹形結(jié)構(gòu)無限極輸出的方法
- thinkphp實(shí)現(xiàn)無限分類(使用遞歸)
- ThinkPHP實(shí)現(xiàn)遞歸無級分類——代碼少
- Thinkphp框架使用list_to_tree 實(shí)現(xiàn)無限級分類列出所有節(jié)點(diǎn)示例
相關(guān)文章
php面向?qū)ο笾衧tatic靜態(tài)屬性與方法的內(nèi)存位置分析
這篇文章主要介紹了php面向?qū)ο笾衧tatic靜態(tài)屬性與方法的內(nèi)存位置,通過內(nèi)存位置實(shí)例分析了static靜態(tài)屬性的原理與使用技巧,需要的朋友可以參考下2015-02-02
php中session過期時間設(shè)置及session回收機(jī)制介紹
在網(wǎng)上可以找到修改配置文件中的session.gc_maxlifetime,如果想了解更多session回收機(jī)制,繼續(xù)閱讀2014-05-05
php mysql操作mysql_connect連接數(shù)據(jù)庫實(shí)例詳解
php操作數(shù)據(jù)庫首先必須連接到指定的數(shù)據(jù)庫,連接數(shù)據(jù)庫可以使用PHP mysql_connect函數(shù),本文章向大家介紹mysql_connect函數(shù)的使用方法和實(shí)例,需要的朋友可以參考一下2016-12-12
WordPress網(wǎng)站訪問慢解決方案細(xì)圖文教程
這篇文章主要介紹了WordPress網(wǎng)站訪問慢解決方案細(xì)圖文教程,wordpress訪問慢一直是一個比較頭疼的問題,有正好需要的同學(xué)可以嘗試下,感覺不錯的可以分享給大家2021-03-03
PHP并發(fā)多進(jìn)程處理利器Gearman使用介紹
這篇文章主要介紹了PHP并發(fā)多進(jìn)程處理利器Gearman使用介紹,需要的朋友可以參考下2016-05-05
PHP運(yùn)行出現(xiàn)Notice : Use of undefined constant 的完美解決方案分享
今天修改公司的網(wǎng)站,提示Notice : Use of undefined constant ,通過下面的方法解決了,最好是error_reporting(0);不需要更改配置2012-03-03

