2

I am using codeigniter framework and i want to create a OR WHERE statement using foreach loop,

Below is my code,

$wh_q = array('ec_date >' => $start, 'ec_date <=' => $end);

This will produce query as WHERE ec_details.date > '2016-11-01 00:00:00' AND ec_details.date <= '2017-01-07 00:00:00'

Now i want to append few more criteria in abover query string with AND ec_details.user_id = '5' OR ec_details.user_id = '6' and so on.

Now user_id, 5 and 6 are coming from array results,

$user_string = '5,6';
$users_array = explode(",",$user_string);

foreach ($users_array as $user) {
   $query = array('ec_details.user_id' => $user);
}

Then i am merging above $query array in original $wh_q array

$wh_q = array_merge($wh_q, $query);

And finally i select where as below,

$this->db->where($wh_q);

But this will not produce my required query, but will just create like below,

`WHERE `ec_details`.`date` > '2016-11-01 00:00:00' AND `ec_details`.`date` <= '2017-01-07 00:00:00'`AND `ec_details`.`user_id` = '6'`

How can i add AND OR WHERE statements using foreach loop in codeigniter?

Thank you,

3
  • I assume you want to get details from ec_detail table for user_id 5 and 6 between start and end range, is that it? Commented Jan 8, 2017 at 6:59
  • @MalikMudassar, yes correct Commented Jan 8, 2017 at 7:01
  • see edited answer...if works accept it thanks... Commented Jan 8, 2017 at 11:17

3 Answers 3

1

Try as below...

$wh_q = array('ec_date >' => $start, 'ec_date <=' => $end);//condition first

$user_string = '5,6';
$users_array = explode(",",$user_string);

foreach ($users_array as $user) {
   $query []= array('ec_details.user_id' => $user);//second condition
}

//print_r($query) //multidimensional array

Query

$this->db->where($wh_q);

$total = count($query);
for($i=0;$i<$total;$i++){
    $this->db->or_where($query[$i]);
    }
Sign up to request clarification or add additional context in comments.

7 Comments

but this will not add value 5, it will just generate -- OR ec_expensedetails.user_id = '6', where as i need -- AND ec_expensedetails.user_id = '5' OR ec_expensedetails.user_id = '6'
what if we use $query.= array('ec_details.user_id' => $user); no, that will create 2 OR statements
@Hikmat, is same key allowed in array? no its not. I think we can only achieve with custom string..
@HikmatSijapati, no it will not work, will produce OR 0 = Array OR 1 = Array
That will still produce two OR statements @Hikmat, but I guess it should get the required results
|
1

You can try like this, create a complete where string like this

$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);

And check, this will work.

EDIT

OR you can give or condition like this

$this->db->where('name !=', $name);
$this->db->or_where('id >', $id);  // Produces: WHERE name != 'Joe' OR id > 50

1 Comment

@V for Vendetta, this can work if we do as string, but we need to pull values from array and create conditional statement.
0

If you you need to check only user id then better you can also user IN like below.

$user_string = '5,6'; $users_array = explode(",",$user_string); 
$this->db->where_in('id', $users_array);

and If you want to use only OR and And statement

then use below code

$this->db->where('ec_date >',$start,false);
$this->db->where('ec_date <=', $end,false);
$user_string = '5,6';
$users_array = explode(",",$user_string);
$userqry = array();
foreach ($users_array as $user) {
   $userqry[] = "ec_details.user_id ='".$user."'";
}
$userqrystr = implode(" OR ",  $userqry);
$this->db->where($userqrystr,false,false);

This query look like

WHERE `ec_details`.`date` > '2016-11-01 00:00:00' AND `ec_details`.`date` <= '2017-01-07 00:00:00' AND (`ec_details`.`user_id` = '6' OR `ec_details`.`user_id` = '5')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.