Welcome Guest, Not a member yet? Register   Sign In
or_where() as first selection statement??
#1

[eluser]mtbkrdave[/eluser]
Hi All-

I'm using the Active Record Class, and I'd like to use the results from database query to select the rows for a second. I query one table for a list of key IDs, then I want to go back to the table to which the keys refer, and select all the original rows.

My question is: Can the $this->db->or_where() function be used as the first function in a series of select functions? I'd like to run through the first query using a foreach() loop, and with each pass through the loop add a new or_where() selection for the second query.

Code:
$this->db->where('id',$key_id);
$query1 = $this->db->get('mytable');
if( $query->num_results() > 0 ){
   foreach($query->result() as $row){
      $this->db->or_where('key',$row->id);
   }
   return $this->db->get('parent_table');
}

Is there a more-efficient way to do this??!

Thanks,
[medic]Dave
#2

[eluser]gullah[/eluser]
you could always do something like this if the or_where doesn't work as the first

Code:
$i=0;
foreach($x as $y)
{
  if($i == 0)
  {
    $this->db->where('bha', 'blah');
  } else {
    $this->db->or_where();
  }
  $i++;
}
#3

[eluser]xwero[/eluser]
or_where can be the first function because the inner working check if there are previous where parts or not.

But if i was you i would get all ids in a comma separated list and use the where_in method this only requires one function call.
#4

[eluser]mtbkrdave[/eluser]
Thank you both!!

[medic]Dave




Theme © iAndrew 2016 - Forum software by © MyBB