Where in/like

3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 15, 2012   (RSS)

Re: [rjbathgate] Where in/like

By Jason - January 13, 2012

Hi,

This can be done using either LIKE or IN, depending on exactly how you want things to work.

You would use IN if you are looking for records where a field has 1 of several values. The field you are comparing, however, needs to have only a single value (like num, for example). To get this to work, what you normally have to do is first break your multi-select value into an array, then use join() to turn it into a comma separated list.

For example:

$practiceAreaList = join(",", explode("\t", trim($record['practice_areas'], "\t")));

if ($practiceAreaList) {
$where = "num IN ($practiceAreaList)";
}


The one catch with IN is that if practice_areas has no value, you will get a MySQL error, which is why we use the if statement.

LIKE works in the exact opposite way. We use LIKE when we have a single value and are looking for it inside of a larger string (like a multi-value field). You were actually very close in your LIKE statement. What you need to do is add tab characters ("\t") to the front and end of your string. Each value in a mult-select list has a tab character in front and in back of it.

Example:

'where' => "practice_areas LIKE '%\t$parent\t%'",

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Where in/like

Hey,

Thanks, the

'where' => "practice_areas LIKE '%\t$parent\t%'",

works perfectly...

Cheers
Rob