簡體   English   中英

如何使用“ if”條件在“ where”條件下在codeigniter中應用過濾器?

[英]How to apply filter in codeigniter in 'where' condition using 'if' condition?

我正在嘗試使用codeigniter應用過濾器,但無法正常工作。 它適用於單個數組記錄,但是當我發送多個逗號分隔的源參數時,它不會過濾。

這是我在模型中的代碼。

    $user_id = $this->input->post('user_id');
    $source = $this->input->post('source');
    $source = explode(',', $source);

    $radius = $this->input->post('radius');

    $this->db->select('post.*, source.*, user.firstname, user.lastname');
    $this->db->from('post');
    $this->db->join('user', 'user.id = post.cop_id');
    $this->db->join('source', 'post.source_id = source.id');
    $this->db->where('post.cop_id', $user_id);
    if (in_array("1", $source)) {
        $this->db->where('post.source_id', 1);
    }
    if (in_array("2", $source)) {
        $this->db->where('post.source_id', 2);
    }

    if (in_array("3", $source)) {
        $this->db->where('post.source_id', 3);
    }

    if (in_array("4", $source)) {
        $this->db->where('post.source_id',4);
    }
    if (!empty($radius)) {
        $this->db->where('post.post_radius', $radius);
    }
    $this->db->where('post.source_id !=', 4);
    $query = $this->db->get();
    return $query->result();

使用where_in

 $this->db->select('post.*, source.*, user.firstname, user.lastname');
    $this->db->from('post');
    $this->db->join('user', 'user.id = post.cop_id');
    $this->db->join('source', 'post.source_id = source.id');
    $this->db->where('post.cop_id', $user_id);
    $this->db->where_in('post.source_id', $source);// it will produse  IN ('1', '2', '3')
    $this->db->where('post.post_radius', $radius);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM