分类加搜索

很实用,分类多了不好查询。

public function index()
{
    if ($parent_name = $_GET['parent_name']) {
        $whereParent['name'] = ['like','%'.$parent_name.'%'];
        $this->assign('parent_name', $parent_name);
    }

    if ($child_name = $_GET['child_name']) {
        $whereChild['name'] = ['like','%'.$child_name.'%'];
        $this->assign('child_name', $child_name);
    }

    $product_type = M('product_type');
    $whereParent['deleted'] = 0;
    $whereParent['pid'] = 0;

    $result = $product_type->where($whereParent)->order('weight desc,id desc')->select();

    foreach ($result as $k=>&$v) {
        // 获取子类
        $whereChild['pid'] = $v['id'];
        $whereChild['deleted'] = 0;
        $child_type = $product_type->where($whereChild)->order('weight desc,id desc')->select();
        if ($child_name && !$child_type) {
            unset($result[$k]);
        }

        $v['child_type'] = $child_type;
    }

    $this->assign('result', $result);
    $this->display();
}

根据父类名称和子类名称,分别查询,能够降低功能复杂度。如果融合到一起,就比较乱。

posted @ 2020-06-18 16:19  TBHacker  阅读(158)  评论(0编辑  收藏  举报