tp取无限级分类
- controller
public function listAction(){
// 判断缓存是否存在
// 初始化缓存配置
S([
"type" => "memcache",
"host" => "127.0.0.1",
"port" => "11211"
]);
$cat_tree = S("cat_tree");
if (false === $cat_tree){
echo "non-cache";
$m_cat = D("Cat");
$cat_tree = $m_cat->getTreeList();
S("cat_tree", $cat_tree);
}
$this->assign("list", $cat_tree, PHP_INT_MAX);
$this->display();
}
public function deleteAction($cat_id){
$m_cat = D("Cat");
if ($m_cat->delete($cat_id)){
S([
"type" => "memcache",
"host" => "127.0.0.1",
"port" => "11211"
]);
S("cat_tree", NULL);
$this->success("删除成功", U("Back/Cat/list"), 0);
} else {
$this->error("删除失败", U("Back/Cat/list"));
}
}
- model
class CatModel extends ThinkModel {
protected $tablename = "category";
public function getTreeList(){
$list = $this->order("sort_order desc")->select();
return $this->_getTree($list, 0, 0);
}
protected function _getTree($rows, $p_id=0, $deep=0){
static $tree = [];
foreach($rows as $row){
if($row["parent_id"] == $p_id){
$row["deep"] = $deep;
$tree[] = $row;
$this->_getTree($rows, $row["cat_id"], 1+$deep);
}
}
}
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 在ThinkPhp中实现无限极分类
- 下一篇: ThinkPHP5.1 @[模块/控制器/]操作