如何在codeigniter中按大小过滤产品

问题描述 投票:0回答:1

我按尺寸过滤产品,我选择使用此jquery功能选择的所有li用户:

var arr_size = [];
i = 0;
$(".filtersize li a").each(function(e) {
    if ($(this).hasClass("selected-filter")) {
        arr_size[i++]= $(this).text();
    }
});

这工作正常,但我将此数组从ajax传递给codeigniter控制器,我将大小数据保存为数据库中的逗号分隔值:

大小栏:

'S', 'M', 'L', 'XL'

和javascript数组还包含例如逗号分隔值。如果用户选择了两个值,它将包含S,M我无法在db中使用like函数搜索数组值是否有任何其他函数或方法来实现这一点我的代码如下:

var data = {id:pid,arr_s:arr_size};
var url = "<?php echo base_url()?>controlle/viewproducts";
var result = post_ajax(url, data);

控制器:

public function viewproducts()
{
    $id=$this->input->post('id');
    $size_arr=$this->input->post('arr_s');
    $result['product'] = $this->product_m->get_product_size_filter($id,$size_arr);
    $this->load->view('product_filter_view/view_product',$result);
}

模型:

public function get_product_size_filter($id,$size) {
    $where_query = '';
    $size_array = explode(',', $size);
    foreach ($size_array as $size_item) {
        $size = "$size_item";
        $where_query .= "dg_products.size LIKE '%$size%' OR ";
    }

    $where_query = rtrim($where_query, " OR ");

    $this->db->select('dg_products.*, AVG(dg_rating.rating) As averageRating');
    $this->db->from('dg_products');
    $this->db->join('dg_rating', 'dg_products.id = dg_rating.product_id','left');
    $this->db->where('dg_products.category_id',$id);
    $this->db->where($where_query);
    $this->db->group_by("dg_products.id");
    $query = $this->db->get();   
    $result = $query->result();
    return $result; 
}
php mysql codeigniter codeigniter-3
1个回答
1
投票

我想,你可以将你的大小字符串转换为数组并运行查询。下面的示例代码:

public function viewproducts() 
{
    $id = $this->input->post('id', true);
    $size_string = $this->input->post('arr_s', true);
    $size_array = explode(',', $size_string);
    $result['product'] = array();

    foreach ($size_array as $size_item) {
        $size = "'$size_item'";
        $result['product'][] = $this->product_m->get_product_size_filter($id, $size);
    }

    $this->load->view('product_filter_view/view_product', $result);
}

如果您不想运行多数据库查询,可以使用:

public function viewproducts() 
{
    $id = $this->input->post('id', true);
    $sizes = $this->input->post('arr_s', true);
    $result['product'] = $this->product_m->get_product_size_filter($id, $sizes);

    $this->load->view('product_filter_view/view_product', $result);
}

这是模型:

public function get_product_size_filter($id, $size)
{
    $where_query = '';
    $size_array = explode(',', $size);

    foreach ($size_array as $size_item) {
        $size = "'$size_item'";
        $where_query .= "size LIKE '%$size%' OR ";
    }

    $where_query = rtrim($where_query, " OR ");

    $this->db->select('*');
    $this->db->from('dg_products');
    $this->db->where($where_query);
    $query = $this->db->get();   
    return $query->result();
}

更新

public function get_product_size_filter($id,$size)
{
    $where_query = '';
    $size_array = explode(',', $size);

    foreach ($size_array as $size_item) {
        $size = "$size_item";
        $where_query .= "dg_products.size LIKE '%$size%' OR ";
    }

    $where_query = rtrim($where_query, " OR ");

    $this->db->select('dg_products.*, AVG(dg_rating.rating) As averageRating');
    $this->db->from('dg_products');
    $this->db->join('dg_rating', 'dg_products.id = dg_rating.product_id','left');
    $this->db->where('dg_products.category_id',$id);
    $this->db->where("($where_query)");
    $this->db->group_by("dg_products.id");
    $query = $this->db->get();   
    $result = $query->result();

    return $result; 
}
© www.soinside.com 2019 - 2024. All rights reserved.