Thinkphp無限級分類代碼
更新時間:2015年11月11日 15:37:18 投稿:lijiao
這篇文章主要介紹了Thinkphp無限級分類代碼,無限級分類真的很重要了,我不會寫怎么辦?本篇就一步步告訴大家如何制作無限級分類,感興趣的小伙伴們可以參考一下
本篇就一點一點教大家寫一個無限級分類出來,其實掌握一個知識,最主要的是要掌握無限級分類的邏輯,那么寫起來就很容易的多了。
首先看數(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{//對應數(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>
顯示結果如下:

有沒有掌握無限級分類的邏輯,上文分享的thinkphp無限級分類代碼,希望對大家的學習有所幫助。
您可能感興趣的文章:
相關文章
php面向對象中static靜態(tài)屬性與方法的內(nèi)存位置分析
這篇文章主要介紹了php面向對象中static靜態(tài)屬性與方法的內(nèi)存位置,通過內(nèi)存位置實例分析了static靜態(tài)屬性的原理與使用技巧,需要的朋友可以參考下2015-02-02
php中session過期時間設置及session回收機制介紹
在網(wǎng)上可以找到修改配置文件中的session.gc_maxlifetime,如果想了解更多session回收機制,繼續(xù)閱讀2014-05-05
php mysql操作mysql_connect連接數(shù)據(jù)庫實例詳解
php操作數(shù)據(jù)庫首先必須連接到指定的數(shù)據(jù)庫,連接數(shù)據(jù)庫可以使用PHP mysql_connect函數(shù),本文章向大家介紹mysql_connect函數(shù)的使用方法和實例,需要的朋友可以參考一下2016-12-12
PHP運行出現(xiàn)Notice : Use of undefined constant 的完美解決方案分享
今天修改公司的網(wǎng)站,提示Notice : Use of undefined constant ,通過下面的方法解決了,最好是error_reporting(0);不需要更改配置2012-03-03

