"memcache", "host" => "127.0.0.1", "port" => "11211" ]); $c" />
牛骨文教育服务平台(让学习变的简单)
博文笔记

tp取无限级分类

创建时间:2017-10-10 投稿人: 浏览次数:385
  • 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);
            }
        }
    }
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。