Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7643725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:31:02+00:00 2026-05-31T09:31:02+00:00

I wrote an active record query in CodeIgniter and then I realised that I

  • 0

I wrote an active record query in CodeIgniter and then I realised that I needed to use OR with to WHERE clauses. So I looked through the docs and found or_where which did what I wanted. But when I use it it produces AND in the output. I couldn’t find any other questions on this issue.

I’m using CodeIgniter: 2.1.0

Here is my code (slightly cut down):

$this->db->select(“p.*”,false);

$this->db->from('projects p');
$this->db->join('customers c', 'p.c_id = c.c_id','left outer');

if(isset($options['add_root']))
  $this->db->or_where('p.p_id=',1,FALSE);

//Get top level projects by default (1) or whatever parent is specified.
if(isset($options['p_id']))
  $this->db->where('p.p_id=',$options['p_id'],false);

$query = $this->db->get();//query
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:31:03+00:00Added an answer on May 31, 2026 at 9:31 am

    I don’t think you need or_where. I think you need better if/else in PHP.

    The logic you probably want:

    if(isset($options['p_id']))
    {
        // if p_id is set, then look for p_id
        $this->db->where('p.p_id=',$options['p_id'],false);
        // if p_id and add_root are set, then look for p_id OR p_id = 1
        if(isset($options['add_root']))
            $this->db->or_where('p.p_id=',1,FALSE);
    }
    elseif(isset($options['add_root']))
    {
        // look for p_id = 1 only
        $this->db->where('p.p_id=',1,FALSE);
    }
    

    Because or_where is first it is simply defaulting to where, and then the subsequent where is the default: an “and”.

    You could also write the above with a series of elseif‘s but I view this as less clear:

    if(isset($options['p_id']) && isset($options['add_root']))
        $this->db
           ->where('p.p_id=',$options['p_id'],false)
           ->or_where('p.p_id=',1,FALSE);
    elseif(isset($options['p_id']) || isset($options['add_root']))
        $this->db
           ->where('p.p_id=',
                   // if add_root is set, then 1, otherwise p_id
                   (isset($options['add_root'])?1:$options['p_id']),false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do i write in codeigniter in active record the following query? select tbl_list.id,
I'm having an issue performing a query for only objects that are active. I
What I Want I want to read the cache of Safari through Active Record.
Can any one help me to write this using rails3 active record query?? SQL-
How to write select query with between similar to this with active record? SELECT
I wrote a windows service using VB that read some legacy data from Visual
I wrote myself a little downloading application so that I could easily grab a
I'm writing a Rails 3 ActiveRecord query using the where syntax, that uses both
According to save bang your head, active record will drive you mad , we
I'm trying to execute this query and when it finds a record for $serial,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.