Welcome Guest, Not a member yet? Register   Sign In
codeigniter query problem
#1

[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 ...
#2

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

[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.
#4

[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();
}
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB