首页 > 其他 > 详细

分类加搜索

时间:2020-06-18 16:40:06      阅读:58      评论:0      收藏:0      [点我收藏+]

技术分享图片

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

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();
}

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

分类加搜索

原文:https://www.cnblogs.com/jiqing9006/p/13158158.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!