当前位置:Gxlcms > PHP教程 > ecshop首页商品属性筛选如何实现

ecshop首页商品属性筛选如何实现

时间:2021-07-01 10:21:17 帮助过:6人阅读

请教各位大神, ecshop商品属性多条件筛选, 只能在商品分类列表页才能实现, 请问如何才能在全站都能实现这个功能呢?
类似京东的全部商品分类, 急求, 试了很多方法都不能成功调用...

回复内容:

请教各位大神, ecshop商品属性多条件筛选, 只能在商品分类列表页才能实现, 请问如何才能在全站都能实现这个功能呢?
类似京东的全部商品分类, 急求, 试了很多方法都不能成功调用...

该问题已经解决, 贴出来给大家分享一下, 如果不正确的地方, 希望指出, 谢谢
把下面这段代码 Copy到程序根目录 index.php里面

 /**
     * 获得分类的信息
     *
     * @param   integer $cat_id
     *
     * @return  void
     */
    function get_cat_info($cat_id)
    {
        return $GLOBALS['db']->getRow('SELECT cat_name, keywords, cat_desc, style, grade, filter_attr, parent_id FROM ' . $GLOBALS['ecs']->table('category') .
            " WHERE cat_id = '$cat_id'");
    }


    /**
     * 获取商品类型属性
     * @param $cat_id   栏目ID
     * @return array 商品属性
     */
    function get_my_class($cat_id)
    {

        $children = get_children($cat_id);

        $cat = get_cat_info($cat_id);   // 获得分类的相关信息

        /* 属性筛选 */
        $ext = ''; //商品查询条件扩展
        if ($cat['filter_attr'] > 0)
        {
            $cat_filter_attr = explode(',', $cat['filter_attr']);       //提取出此分类的筛选属性
            $all_attr_list = array();

            foreach ($cat_filter_attr AS $key => $value)
            {
                $sql = "SELECT a.attr_name FROM " . $GLOBALS['ecs']->table('attribute') . " AS a, " . $GLOBALS['ecs']->table('goods_attr') . " AS ga, " . $GLOBALS['ecs']->table('goods') . " AS g WHERE ($children OR " . get_extension_goods($children) . ") AND a.attr_id = ga.attr_id AND g.goods_id = ga.goods_id AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND a.attr_id='$value'";
                if($temp_name = $GLOBALS['db']->getOne($sql))
                {
                    $all_attr_list[$key]['filter_attr_name'] = $temp_name;

                    $sql = "SELECT a.attr_id, MIN(a.goods_attr_id ) AS goods_id, a.attr_value AS attr_value FROM " . $GLOBALS['ecs']->table('goods_attr') . " AS a, " . $GLOBALS['ecs']->table('goods') .
                        " AS g" .
                        " WHERE ($children OR " . get_extension_goods($children) . ') AND g.goods_id = a.goods_id AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 '.
                        " AND a.attr_id='$value' ".
                        " GROUP BY a.attr_value";

                    $attr_list = $GLOBALS['db']->getAll($sql);

                    $temp_arrt_url_arr = array();

                    for ($i = 0; $i < count($cat_filter_attr); $i++)        //获取当前url中已选择属性的值,并保留在数组中
                    {
                        $temp_arrt_url_arr[$i] = !empty($filter_attr[$i]) ? $filter_attr[$i] : 0;
                    }

                    $temp_arrt_url_arr[$key] = 0;                           //“全部”的信息生成
                    $temp_arrt_url = implode('.', $temp_arrt_url_arr);
                    $all_attr_list[$key]['attr_list'][0]['attr_value'] = $_LANG['all_attribute'];
                    $all_attr_list[$key]['attr_list'][0]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_min, 'price_max'=>$price_max, 'filter_attr'=>$temp_arrt_url), $cat['cat_name']);
                    $all_attr_list[$key]['attr_list'][0]['selected'] = empty($filter_attr[$key]) ? 1 : 0;

                    foreach ($attr_list as $k => $v)
                    {
                        $temp_key = $k + 1;
                        $temp_arrt_url_arr[$key] = $v['goods_id'];       //为url中代表当前筛选属性的位置变量赋值,并生成以‘.’分隔的筛选属性字符串
                        $temp_arrt_url = implode('.', $temp_arrt_url_arr);

                        $all_attr_list[$key]['attr_list'][$temp_key]['attr_value'] = $v['attr_value'];
                        $all_attr_list[$key]['attr_list'][$temp_key]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_min, 'price_max'=>$price_max, 'filter_attr'=>$temp_arrt_url), $cat['cat_name']);

                        if (!empty($filter_attr[$key]) AND $filter_attr[$key] == $v['goods_id'])
                        {
                            $all_attr_list[$key]['attr_list'][$temp_key]['selected'] = 1;
                        }
                        else
                        {
                            $all_attr_list[$key]['attr_list'][$temp_key]['selected'] = 0;
                        }
                    }
                }

            }

            /* 扩展商品查询条件 */
            if (!empty($filter_attr))
            {
                $ext_sql = "SELECT DISTINCT(b.goods_id) FROM " . $GLOBALS['ecs']->table('goods_attr') . " AS a, " . $GLOBALS['ecs']->table('goods_attr') . " AS b " .  "WHERE ";
                $ext_group_goods = array();

                foreach ($filter_attr AS $k => $v)                      // 查出符合所有筛选属性条件的商品id */
                {
                    if (is_numeric($v) && $v !=0 &&isset($cat_filter_attr[$k]))
                    {
                        $sql = $ext_sql . "b.attr_value = a.attr_value AND b.attr_id = " . $cat_filter_attr[$k] ." AND a.goods_attr_id = " . $v;
                        $ext_group_goods = $GLOBALS['db']->getColCached($sql);
                        $ext .= ' AND ' . db_create_in($ext_group_goods, 'g.goods_id');
                    }
                }
            }

            return $all_attr_list;
        }
    }

调用的时候

$all_attr_list = get_my_class(22);
$smarty->assign('woshi',  $all_attr_list);

人气教程排行