繁体   English   中英

如何在codeigniter中显示在电子商务站点中产品所应用的过滤器数量?

[英]How to show number of filters are applied for products in eCommerce site in codeigniter?

我在网站上为产品添加了一些过滤器(例如价格,品牌,类别等),现在我想显示应用了多少个过滤器

这是查看页面:

<div class="btn btn-info" data-toggle="modal" data-target="#myModal"  style="width:130px;">
   <div class="list-updtr__img"></div>
   <div class="list-updtr__lbl" style="font-size:12px;">FILTER BY</div>
   <div class="list-updtr__val" style="font-size: 10px;margin-left: 18px;">0 filters applied</div>
</div>

这是我的控制器:

if ($this->input->get_post('deal_type')) {
    $this->data['current_deal_type'] = explode('-', $this->input->get_post('deal_type'));
    $filters['deals.deal_type'] = $this->data['current_deal_type'];
}
if ($this->input->get_post('tags')) {
    $this->data['current_tags'] = explode('-', $this->input->get_post('tags'));
    $filters['deals.tags'] = $this->data['current_tags'];
}
if ($this->input->get_post('s')) {
    $filters['deals.deal_title'] = $this->input->get_post('s');
}
if ($this->input->get_post('min_price')) {
    $filters['deals.deal_price >='] = $this->input->get_post('min_price');
}
if ($this->input->get_post('max_price')) {
    $filters['deals.deal_price <='] = $this->input->get_post('max_price');
}
/* External filters ends here */
/* sort filters starts here */
$sort_filters = array('deals.position_number' => 'ASC');
if ($this->input->get_post('sort')) {
    if ($this->input->get_post('sort') == 'popularity-desc') {
    //Need add script after implementing views count for each deal
    } elseif ($this->input->get_post('sort') == 'price-asc') {
    $sort_filters = array('deals.deal_price' => 'ASC');
    } elseif ($this->input->get_post('sort') == 'price-desc') {
    $sort_filters = array('deals.deal_price' => 'DESC');
    } elseif ($this->input->get_post('sort') == 'newest-desc') {
    $sort_filters = array('deals.created_date_time' => 'DESC');
    }
}

这是从数据库获取过滤器列表的模型

function get($filters = [], $sort_filters = [], $limit = 0, $offset = 0) {
$this->db->select('deals.*,categories.category_name,categories.seo_url as category_seo_url,sub_categories.sub_category_name,sub_categories.seo_url as sub_category_seo_url,stores.store_name,stores.is_direct_link');
$this->db->from('deals');
$this->db->join('categories', 'categories.id=deals.categories_id');
$this->db->join('sub_categories', 'sub_categories.id=deals.sub_categories_id');
$this->db->join('stores', 'stores.id=deals.stores_id');
$this->db->where("deals.status", 1);
foreach ($filters as $key => $value) {
    if (is_array($value)) {
    if ($key == 'deals.tags') {
        $cond = array();
        foreach ($value as $key => $val) {
        $cond[] = 'FIND_IN_SET("' . $val . '",deals.tags) != 0';
        }
        $this->db->where('(' . implode(' OR ', $cond) . ')');
    } else {
        $key_array = explode(' ', $key);
        if (isset($key_array[1]) && ($key_array[1] == "!=")) {
        $this->db->where_not_in($key_array[0], $value);
        } else {
        $this->db->where_in($key, $value);
        }
    }
    } elseif ($key == 'deals.deal_title') {
    $this->db->like($key, $value);
    } elseif ($key == 'deals.tags') {
    $this->db->where('FIND_IN_SET("' . $value . '",deals.tags) !=', 0);
    } else {
    $this->db->where($key, $value);
    }
}
foreach ($sort_filters as $key => $value) {
    $this->db->order_by($key, $value);
}
if ($limit != 0) {
    if ($offset != 0) {
    $this->db->limit($limit, $offset);
    } else {
    $this->db->limit($limit);
    }
}
$data = $this->db->get()->result();
if ($data) {
    foreach ($data as $item) {
    if ($item->current_status == 'Active') {
        $item->current_status_bootstrap_class = 'primary';
    } else {
        $item->current_status_bootstrap_class = 'danger';
    }
    if ($item->deal_type == 'DEAL') {
        $item->deal_type_bootstrap_class = 'primary';
        $item->deal_type_text = 'Deal';
    } else {
        $item->deal_type_bootstrap_class = 'success';
        $item->deal_type_text = 'Offer & Coupon';
    }
    $item->image = FILE_UPLOADED_PATH . $item->image;
    }
    return $data;
}
return [];
}

我在上面的代码中在哪里添加过滤器计数,我需要在应用过滤器时设置该计数需要增加的标志

首先,您需要在实际控制器开始像这样工作之前声明会话变量,

$_SESSION['filters_counter'] = 0;

现在,每当任何用户应用任何过滤器时,都只需像这样在控制器内部增加filter_counter变量,

if ($this->input->get_post('deal_type')) {
    $this->data['current_deal_type'] = explode('-', $this->input->get_post('deal_type'));
    $filters['deals.deal_type'] = $this->data['current_deal_type'];
    $_SESSION['filters_counter'] += 1;
}
if ($this->input->get_post('tags')) {
    $this->data['current_tags'] = explode('-', $this->input->get_post('tags'));
    $filters['deals.tags'] = $this->data['current_tags'];
    $_SESSION['filters_counter'] += 1;
}
if ($this->input->get_post('s')) {
    $filters['deals.deal_title'] = $this->input->get_post('s');
    $_SESSION['filters_counter'] += 1;
}
if ($this->input->get_post('min_price')) {
    $filters['deals.deal_price >='] = $this->input->get_post('min_price');
    $_SESSION['filters_counter'] += 1;
}
if ($this->input->get_post('max_price')) {
    $filters['deals.deal_price <='] = $this->input->get_post('max_price');
    $_SESSION['filters_counter'] += 1;
}
/* External filters ends here */
/* sort filters starts here */
$sort_filters = array('deals.position_number' => 'ASC');
if ($this->input->get_post('sort')) {
    if ($this->input->get_post('sort') == 'popularity-desc') {
    //Need add script after implementing views count for each deal
    } elseif ($this->input->get_post('sort') == 'price-asc') {
    $sort_filters = array('deals.deal_price' => 'ASC');
    } elseif ($this->input->get_post('sort') == 'price-desc') {
    $sort_filters = array('deals.deal_price' => 'DESC');
    } elseif ($this->input->get_post('sort') == 'newest-desc') {
    $sort_filters = array('deals.created_date_time' => 'DESC');
    }
    $_SESSION['filters_counter'] += 1;
}

在视图内部,您可以像这样显示过滤器计数器,

<div class="btn btn-info" data-toggle="modal" data-target="#myModal"  style="width:130px;">
   <div class="list-updtr__img"></div>
   <div class="list-updtr__lbl" style="font-size:12px;">FILTER BY</div>
   <div class="list-updtr__val" style="font-size: 10px;margin-left: 18px;">$_SESSION['filters_counter'] filters applied</div>
</div>

我希望这可以帮助你...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM