user_777
user_777

Reputation: 805

how can we use or_where_in along with other where condition in codeigniter

Here am stuck with the or_where_in condition.while using this condition the result is obtaining correctly here is my code please have a look

public function get_date_wise_sales_info($start_date, $end_date,$type=NULL,$user_id=NULL,$get_all_user_ids=NULL)
{
    if(!empty($type))
    {
        $this->db->where('type',$type);
    }
    if(!empty($user_id))
    {
        $this->db->where('user_id', $user_id);
    }
    if(!empty($get_all_user_ids))
    {
        $this->db->or_where_in('user_id',$get_all_user_ids);
    }

    $this->db->where("DATE(bills.created_at) BETWEEN '{$start_date}' AND '{$end_date}'");

    $this->db->order_by('bills.id','desc');
    $query = $this->db->get('bills');
    return $query->result();
}

here by using or_where_in condition the results of not selected dates are coming and if i remove or_where_in the result is obtaining correctly.how to write the correct syntax of using or_where_in.i wrote where_in instead of or_where_in but nothing is getting displayed and am using codeigniter 3.1.5 version

Upvotes: 0

Views: 164

Answers (1)

Indigo
Indigo

Reputation: 853

I don't know if it's what you are trying to do, but because of the OR statement you may need to use query grouping.

public function get_date_wise_sales_info($start_date, $end_date,$type=NULL,$user_id=NULL,$get_all_user_ids=NULL)
{
    if(!empty($type))
    {
        $this->db->where('type',$type);
    }

    if(!empty($user_id) && !empty($get_all_user_ids))
    {
        // Check for both, with OR
        $this->db->group_start();
        $this->db->where('user_id', $user_id);
        $this->db->or_where_in('user_id',$get_all_user_ids);
        $this->db->group_end();
    } 
    else if(!empty($user_id))
    {
        // Only check for $user_id
        $this->db->where('user_id', $user_id);
    }
    else if(!empty($get_all_user_ids))
    {
        // Only check for $get_all_user_ids
        $this->db->where_in('user_id', $get_all_user_ids);
    }

    $this->db->where("DATE(bills.created_at) BETWEEN '{$start_date}' AND '{$end_date}'");

    $this->db->order_by('bills.id','desc');
    $query = $this->db->get('bills');
    return $query->result();
}

EDIT: this is a bit more complex than simply adding grouping. Updated to check for all possibilities.

Upvotes: 1

Related Questions