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)”
- if there are no OR statements, then I don’t need the AND
- 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 (
Codeigniter’s “ActiveRecord” (airquotes…) Class is fairly limited and doesn’t do well with more advanced
AND/ORscoping, and points you back to raw sql for “more advanced” queries.I would formulate your own
WHEREstring to build the specific query you are wanting and then just useexample…