doudouchan5830 2018-12-18 09:30
浏览 28
已采纳

如何根据在MULTISELECT上选择的数据在codeigniter中循环OR WHERE

I want to make a data filter feature using the MULTISELECT category where the data that appears is in accordance with the list of categories selected on the filter. So, I plan to loop the OR WHERE command according to the list of categories chosen by the user. OR WHERE has been successfully loop according to the number of categories chosen, but not for the ID. And show error like this "Array to string conversion"

What is the cause and what is the solution? Thank you


My Controller

$sort = $this->input->post('sort');
$category = $this->input->post("get_kategori_acara_css[]");
$interest = $this->input->post("get_interest_acara_css[]");

/* eksekusi library pagination ke model penampilan data */
if (!empty($sort) || !empty ($category) || !empty ($interest))
{ 
  $this->data['list_acara'] = $this->Acara_model->get_filtered($sort, $category, $interest, $config['per_page'], $dari);
}
else 
{
  $this->data['list_acara'] = $this->Acara_model->get_all_arsip();
}

MY MODEL

public function get_filtered ($sort, $category, $interest) {
$this->db->join('kategori', 'acara.id_kategori = kategori.id_kategori');
$this->db->join('interest_acara ', 'acara.id_acara  = interest_acara.id_acara', 'left');
$this->db->join('hits_acara  ', 'acara.judul_seo = hits_acara.judul_seo', 'left');
$this->db->where('publish','Ya');
$this->db->group_by('acara.judul_seo');
if(!empty($sort AND empty($category) AND empty($interest))) 
{
  $this->db->order_by($sort, $this->order);
}
elseif(!empty($sort) AND !empty($category) AND empty($interest)) 
{
  foreach ($category as $value) {
    $this->db->or_where('acara.id_kategori', $value->id_kategori);
  }
}
return $this->db->get($this->table)->result();
}

Shown Error

My Multiselect Feature

  • 写回答

1条回答 默认 最新

  • duanbei8904 2018-12-18 17:15
    关注

    The problem is the use of $value->id_kategori in the foreach ($category as $value) loop.

    The variable $value is not an object so trying to get the value of the property, i.e. $value->id_kategori makes no sense.

    $category is a simple indexed array of values e.g.

    array('0'=>'123', '1'=>'456', '2'=>'789')
    

    I think all you need to do is change this

    $this->db->or_where('acara.id_kategori', $value->id_kategori);
    

    to this

    $this->db->or_where('acara.id_kategori', $value);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?