Welcome Guest, Not a member yet? Register   Sign In
Help nesting active record where statements
#1

[eluser]rt30000[/eluser]
I am trying to build an active record query and I am having a bit of trouble with my where statements. You will notice I want the widget_id portion to be nested if set, such as 'AND WHERE(1=x OR 2=x OR 3=x)'

The model snippet...
Code:
$this->db->select('page_content.id, page_content.name, categories.name AS category_name', FALSE);
$this->db->orderby('category_name', 'asc');
$this->db->orderby('name', 'asc');
$this->db->where('client_managed','Yes');
if ($cat_id!=0)
{
     $this->db->where('page_content.parent_category_id', $cat_id);
}
if ($widget_id!=1)
{
      //$where = "('page_content.optional_widget_1'='$widget_id' OR 'page_content.optional_widget_2'='$widget_id' OR 'page_content.optional_widget_3'='$widget_id')";
      //this->db->where($where, NULL, FALSE);
      $this->db->where('optional_widget_1', $widget_id);
      $this->db->or_where('optional_widget_2', $widget_id);
      $this->db->or_where('optional_widget_3', $widget_id);
}
$this->db->from('page_content');
$this->db->join('categories','page_content.parent_category_id = categories.id', 'LEFT OUTER');
$query = $this->db->get();    
        
echo $this->db->last_query();
Here is the resulting query:
Code:
SELECT page_content.id, page_content.name, categories.name AS category_name FROM (page_content) LEFT OUTER JOIN categories ON page_content.parent_category_id = categories.id WHERE `client_managed` = 'Yes' AND `page_content`.`parent_category_id` = '3' AND `optional_widget_1` = '2' OR `optional_widget_2` = '2' OR `optional_widget_3` = '2' ORDER BY category_name asc, name asc
#2

[eluser]Damien K.[/eluser]
Your commented out code would be my first approach in solving your group condition problem...
#3

[eluser]rt30000[/eluser]
Thank you for responding. That is actually how I solved the problem. At first it wouldn't work correctly but it turned out to be a bit of problematic code in the controller that called it, not the commented out code. Thanks




Theme © iAndrew 2016 - Forum software by © MyBB