Welcome Guest, Not a member yet? Register   Sign In
Problem with query
#1

[eluser]jacobson[/eluser]
Hello I have a problem with my website. I have 3 tables.
Table 1 Consists of id's from Table 2 and 3

it's hard to describe it with words ;p
Code:
$q = $this->db->select('*')->from('table1')->join('table2', 'table1.category = table2.id', 'left')->join('table3', 'table1.news= table3.id', 'left')->where_in('category', $categories)->get();

$categories is an array of id's. My problem is when I have for example 3 id's in my $categories it shows everything properly. The same with 2 id's and 1 id. Problem appears when I pass an empty $categories array because then my statement returns so many records with undefined fields.

Mabe I did a mistake in my query. I would appreciate your help with this one Smile
#2

[eluser]CroNiX[/eluser]
Maybe check to see if your $categories is empty before adding it to the query?

Code:
$this->db
  //->select('*')    //don't really need this.  If you don't have a select it will select * by default
  //->from('table1') //don't really need this either.  It can be put in the "get" portion saving you another method call.
  ->join('table2', 'table1.category = table2.id', 'left')
  ->join('table3', 'table1.news= table3.id', 'left');

//only add the where_in if there are categories
if (count($categories))
{
  $this->db->where_in('category', $categories);
}

//Run the final query
$q = $this->db->get('table1'); //putting table name here is same as having a "from" but with less code




Theme © iAndrew 2016 - Forum software by © MyBB