Welcome Guest, Not a member yet? Register   Sign In
how to concat query conditions using if statements
#1

[eluser]max123[/eluser]
I have a sql string in this way

Code:
$sql="select * from author where price=50 AND type=5";

if($checked1==1)
{
    $sql.="AND (";

    if($checked2==2)
   {
        $sql.="cat=5";
    }

  if($checked3==3)
   {
        $sql.=" or cat=10";
    }

   $sql.=")";

}

how can I write this kind of a sql using active record class
#2

[eluser]mddd[/eluser]
Code:
$this->db->where('price', 50);
$this->db->where('type', 5);

if ($checked1==1)
{
   $cats = array();
   if ($checked2==2)
   {
      $cats[]=5;
      if ($checked3==3) $cats[]=10;
      $this->db->where_in('cat', $cats);
   }
}

$this->db->get('author');
#3

[eluser]max123[/eluser]
Thanx. But what if I have to check whether the 'cat' is available within 2 columns of 2 tables. how can I create the array to check 'where_in'
#4

[eluser]mddd[/eluser]
You'll have to make a where_in call for each of those tables.

If you want to see any of the two fields matches THE SAME list of cats
(meaning: if 'table1.cat' is equal to 1 or 2, or 'table2.cat' is equal to 1 or 2):
Code:
// .. first make the $cats array like before
$cats = array(1,2);
// now check it against more than 1 table
$this->db->where_in('table1.cat', $cats);
$this->db->or_where_in('table2.cat', $cats);

If you want to see if any of the two fields matches A DIFFERENT list of cats
(meaning: if 'table1.cat' is equal to 1 or 2, or table2.cat' is equal to 3 or 4):
Code:
// .. first make an array for each table you want to check
$cats1 = array(1,2);
$cats2 = array(3,4);
// now check it against more than 1 table
$this->db->where_in('table1.cat', $cats1);
$this->db->or_where_in('table2.cat', $cats2);




Theme © iAndrew 2016 - Forum software by © MyBB