CodeIgniter Forums
problem in $this->db->or_where() - 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: problem in $this->db->or_where() (/showthread.php?tid=41260)

Pages: 1 2


problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]Bigil Michael[/eluser]
Quote:function get_ordinary_search($search, $category)
{
if($search!='all'){
$this->db->like('heading', $search);
}
$this->db->where('featured', '1');
$this->db->where('status', '1');
$this->db->where('cat_id', (int)$category);
$this->db->or_where('subcat_id', (int)$category);
$result_category_newss = $this->db->get('classifieds');
return $result_category_newss->result();
}
this is the model function i used to do search.
without $this->db->or_where('subcat_id', (int)$category);
it is working well

when i use the condition

$this->db->or_where('subcat_id', (int)$category);

it shows every fields in the table

can anyone help me???/
is there any syntax problem....

please reply
thanks in advance..


problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]Sudz[/eluser]
Now Try this one
Code:
function get_ordinary_search($search, $category)
  {
      if($search!=‘all’){
        $this->db->like(‘heading’, $search);
      }
      $this->db->where(‘cat_id’, (int)$category);
      $this->db->or_where(‘subcat_id’, (int)$category);
      $this->db->where(‘featured’, ‘1’);
      $this->db->where(‘status’, ‘1’);
      
      $result_category_newss = $this->db->get(‘classifieds’);
      return $result_category_newss->result();
  }



problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]Bigil Michael[/eluser]
problem is not solved

when i use
Quote:$this->db->or_where('subcat_id', (int)$category);

like statement is not working


can anyone help me

urgent......


problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]danmontgomery[/eluser]
AR doesn't group where clauses at all. So, your query says:

Code:
SELECT * FROM `classifieds` WHERE `featured` = 1 AND `status` = 1 AND `cat_id` = $category OR `subcat_id` = $category

which will give you any thing with subcat_id = $category. You need to manually write the where clause if you want it grouped.

Code:
if($search!=‘all’){
    $this->db->like('heading', $search);
}
$this->db->where('(cat_id = '.(int)$category.' OR subcat_id = '.(int)$category.')', NULL, FALSE);
$this->db->where('featured', 1);
$this->db->where('status', 1);



problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]Bigil Michael[/eluser]
but it shows no result


problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]danmontgomery[/eluser]
so use:
Code:
$this->output->enable_profiler();
to see what queries are being run, find the query, run it in phpmyadmin and see what the problem is


problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]Sudz[/eluser]
Put you or_where condition first then try it.
like this
Code:
function get_ordinary_search($search, $category)
  {
    
      $this->db->where(‘cat_id’, (int)$category);
      $this->db->or_where(‘subcat_id’, (int)$category);
      
     $this->db->where(‘featured’, ‘1’);
      $this->db->where(‘status’, ‘1’);
       if($search!=‘all’){
        $this->db->like(‘heading’, $search);
      }
      $result_category_newss = $this->db->get(‘classifieds’);
      return $result_category_newss->result();
  }



problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]Bigil Michael[/eluser]
but it will not echo the query

i applied like this
Quote:function get_ordinary_search($search, $category)
{
if($search!='all'){
$this->db->like('heading', $search);
}
$this->db->where('(cat_id = '.(int)$category.' OR subcat_id = '.(int)$category.')', NULL, FALSE);
$this->db->where('featured', '1');
$this->db->where('status', '1');

$result_category_newss = $this->db->get('classifieds');
$this->output->enable_profiler();
echo $result_category_newss;
return $result_category_newss->result();

}
and it shows some error


problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]Bigil Michael[/eluser]
Quote:function get_ordinary_search($search, $category)
{

$this->db->where(‘cat_id’, (int)$category);
$this->db->or_where(‘subcat_id’, (int)$category);

$this->db->where(‘featured’, ‘1’);
$this->db->where(‘status’, ‘1’);
if($search!=‘all’){
$this->db->like(‘heading’, $search);
}
$result_category_newss = $this->db->get(‘classifieds’);
return $result_category_newss->result();
}
it also has same problem


problem in $this->db->or_where() - El Forum - 05-03-2011

[eluser]Bigil Michael[/eluser]
can anyone help me????
urgent.....