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 6323521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:32:05+00:00 2026-05-24T16:32:05+00:00

Based on user selections, I need to filter results in my query, but I’m

  • 0

Based on user selections, I need to filter results in my query, but I’m stuck on how to correctly implement dynamic OR statements after my AND. I’m currently using Active Record in CodeIgniter, but this may have to change.

I essentially need to create the following snippet: “WHERE city.id = 9 AND (eventType = 8 or eventType = 9)”

  1. if there are no OR statements, then I don’t need the AND
  2. there could be 1-n OR statements

My code currently is as follows:

$this->db->where('city.id =', $cityID);

    if ($eventTypes != NULL){
               foreach ($eventTypes as $item){
                    $eventTypeID = intval($item); 
                    $this->db->or_where('eventtype.id =', $eventTypeID);
               }
           }

This produces: WHERE city.id = 13 OR eventtype.id = 6 OR eventtype.id = 8 … so I need the AND (

  • 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-24T16:32:05+00:00Added an answer on May 24, 2026 at 4:32 pm

    Codeigniter’s “ActiveRecord” (airquotes…) Class is fairly limited and doesn’t do well with more advanced AND/OR scoping, and points you back to raw sql for “more advanced” queries.

    I would formulate your own WHERE string to build the specific query you are wanting and then just use

    $this->db->where($sql)
    

    example…

    $where = "city.id = $cityID ";
    
    if ($eventTypes != NULL)
    {
        $where .= "AND ( ";
        foreach ($eventTypes as $i => $type)
        {
            $eventTypeID = intval($type);
    
            // if it's not the first element
            // (assumes $eventTypes is non-associative)
            if ($i !== 0)
            {
                $where .= "OR ";
            }
            $where .= "eventtype.id = $eventTypeID ";
        }
        $where .= " ) ";
    }
    
    $this->db->where($where);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a user-based website. For each user, I'll need a few MySQL tables
i will need to generate a report based on user input for every food
I'm trying to change view controllers based on a user's selection in a UIAlertView
the company I work for has a web-based user interface which paying clients log
Based on the data model below And based on user input I create a
I am working on a user based social network. I am building the site
I'm setting up a group / user based security system. I have 4 tables
I'm about to create a user based website and will have to store photo,
I'm trying to generate prime numbers based on user input. This is what I
I'm trying to disable/enable controls based on user permission using a custom security framework

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.