"memcache", "host" => "127.0.0.1", "port" => "11211" ]); $c" />
入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

tp取无限级分类

创建时间:2017-10-10 投稿人: 浏览次数:362
  • 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);
            }
        }
    }
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像