Welcome Guest, Not a member yet? Register   Sign In
Build a MySQl query checking if one value of a coma delimited set of values table field matches an array of values condi
#1

[eluser]jvicab[/eluser]
Have you ever used CI where_in function, when a field should be checked against a set of values? I am sure you have, but what about if that field holds a set of values instead of only one, and we would like to get all the rows that has at least one match between the values stored on the field and the values on the condition array?

I have been several times in that situation but CI doesn't have this function in DB_active_rec.php, so I decided to do something about it.
There is a function I have developed to do this, I mean, it allows us to build a query that checks if any of the element in the $condarr is an element of the comma delimited set of values of $setfield.

Here is the function:

Code:
function where_in_set($setfield, $condarr)
{
  $arr = array();    
  foreach ($condarr as $item)
    $arr[] = "^$item$|^$item,|,$item$|,$item$,";
  $condstr = implode('|', $arr);
  $condstr = '(' . $condstr . ')';    
  $setfield .= ' REGEXP';    
  $this->where($setfield, $condstr);
}

Open DB_active_rec.php (located on your_application/system/database/) and copy the code the function inside the class, and use it in your code like $this->db->where_in_set($yourtablefieldname, $yourconditionarray);

I hope it would help you as it has to me.




Theme © iAndrew 2016 - Forum software by © MyBB