CodeIgniter Forums
codeigniter query problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: codeigniter query problem (/showthread.php?tid=57015)



codeigniter query problem - El Forum - 02-07-2013

[eluser]Bigil Michael[/eluser]
my code
Code:
function lekhanam($lekhanam) {

  $lek = explode(',', $lekhanam);
  
  if (!empty($lek))
  {  
   foreach ($lek as $a1)
   {    
    $this->db->or_where('a.id',$a1);    
   }  
  }
  $this->db->limit(5);  
  $this->db->where('a.status',1);
  $this->db->where('at.status',1);
  $this->db->select('a.*, ac.category_name, at.id as author_id, at.name, at.photo as authorimage');
     $this->db->from('articles AS a');
     $this->db->join('article_categories AS ac', 'ac.category_id = a.category_id');
     $this->db->join('authors AS at', 'at.id = a.author_id');
    
     $result= $this->db->get();
      return $result->result();
}
here problem is status not working.
so i print the query
Code:
SELECT `a`.*, `ac`.`category_name`, `at`.`id` as author_id, `at`.`name`, `at`.`photo` as authorimage FROM (`articles` AS a) JOIN `article_categories` AS ac ON `ac`.`category_id` = `a`.`category_id` JOIN `authors` AS at ON `at`.`id` = `a`.`author_id` WHERE `a`.`id` = '7' OR `a`.`id` = '8' OR `a`.`id` = '9' OR `a`.`id` = '10' OR `a`.`id` = '11' AND `a`.`status` = 1 AND `at`.`status` = 1 LIMIT 5
when i put a bracket between where and AND like this
Code:
SELECT `a`.*, `ac`.`category_name`, `at`.`id` as author_id, `at`.`name`, `at`.`photo` as authorimage FROM (`articles` AS a) JOIN `article_categories` AS ac ON `ac`.`category_id` = `a`.`category_id` JOIN `authors` AS at ON `at`.`id` = `a`.`author_id` WHERE (`a`.`id` = '7' OR `a`.`id` = '8' OR `a`.`id` = '9' OR `a`.`id` = '10' OR `a`.`id` = '11') AND `a`.`status` = 1 AND `at`.`status` = 1 LIMIT 5

It runs fine .

I dont know how to put a bracket in codeigniter query.
can any one modify this query..
thanks ...


codeigniter query problem - El Forum - 02-07-2013

[eluser]Bigil Michael[/eluser]
can any one solve this issue?????????


codeigniter query problem - El Forum - 02-07-2013

[eluser]Aken[/eluser]
You have three options:

1) Write the entire query manually.
2) Write the whole OR block of your where (everything between the parentheses) as one $this->db->where() call.
3) Use the 3.0 development version of CI, that supports grouping where() clauses.


codeigniter query problem - El Forum - 02-08-2013

[eluser]Bigil Michael[/eluser]
thanks for your reply.

solved my problem like this way

Code:
function lekhanam($lekhanam) {
  
  
  $lek = explode(',', $lekhanam);
  $temp='';
  if (!empty($lek))
  {  
   foreach ($lek as $a1)
   {    
    $temp[]= ' a.id = '.$a1;
   }  
  }
  $temp2 = implode(' or ', $temp);
  $where = "(".$temp2.")";
  
  $this->db->limit(5);
  $this->db->where($where);
  $this->db->where('a.status',1);
  $this->db->where('at.status',1);
  $this->db->select('a.*, ac.category_name, at.id as author_id, at.name, at.photo as authorimage');
     $this->db->from('articles AS a');
     $this->db->join('article_categories AS ac', 'ac.category_id = a.category_id');
     $this->db->join('authors AS at', 'at.id = a.author_id');
     $result= $this->db->get();
    return $result->result();
}



codeigniter query problem - El Forum - 02-10-2013

[eluser]rjsmith[/eluser]
why not use 'IN' instead of multiple 'OR'