Mulit Select Dropdown search

4 posts by 3 authors in: Forums > CMS Builder
Last Post: March 7, 2010   (RSS)

By concrete - February 25, 2010 - edited: February 25, 2010

I can't seem to figure out a way to build a search form using a multi select list box.

I would like the user to be able to select multiple cities to search our listings

Re: [concrete] Mulit Select Dropdown search

Is this what you're looking for? (Use Ctrl to select multiple cities)

<select multiple="multiple" size="2">
<option value="ottawa">Ottawa</option>
<option value="montreal">Montreal</option>
<option value="toronto">Toronto</option>
<option value="vancouver">Vancouver</option>
</select>

--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

Re: [concrete] Mulit Select Dropdown search

By Dave - February 25, 2010

Hi concrete,

CMS Builder's automatic searching features don't support multi-value fields or "OR" searches yet.

You can do it with some custom PHP and a MySQL 'where' though. Here's how:

Create a multi-select like northernpenguin showed:

<select name="myMultiSearch[]" multiple="multiple" size="4">
<option>value 1</option>
<option>value 2</option>
<option>value 3</option>
</select><br/>


The key parts are:
- HTML needs multiple="multiple" to allow multiple values
- PHP needs [] on the end of the fieldname to receive multiple values
- The size="4" indicates the height in rows of the select box.

Next, you need to loop over those values in your PHP and generate some MySQL. Here's the code for that:

// create multi field where
$where = '';
if (@$_REQUEST['myMultiSearch']) {
$where = "0";
foreach ($_REQUEST['myMultiSearch'] as $value) {
$where .= mysql_escapef(" OR `cmsfield` = ? ", $value);
}
$where = "($where)";
}


What this code does is create a where like this: (0 OR `cmsfield` = 'value 2' OR `cmsfield` = 'value 3' )
Make sure to rename 'cmsfield' to the field you want searched.

Finally, pass this code to getRecords() with this option:

'where' => $where,

Also, be prepared to spend a little time experimenting to get this working correctly. Preparing for that upfront can save a lot of frustration.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com