我正在按尺寸过滤产品,我选择了li的所有用户都选择了 使用此jquery函数:
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中搜索数组值,是否还有其他函数或方法可以实现此目的?我的代码如下:
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;
}
答案 0 :(得分:1)
我认为,您可以将您的 size 字符串转换为数组并运行查询。 下面的示例代码:
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;
}