Search with checkbox only shows “last” selected item in search box

7 posts by 3 authors in: Forums > CMS Builder
Last Post: February 1, 2012   (RSS)

By (Deleted User) - January 29, 2012

[font "Calibri"]This is my search page:
[/#000000]
[url "http://www.koshertravelinfo.com/kosher-hotels/search-advanced.html"][font "Calibri"]http://www.koshertravelinfo.com/kosher-hotels/search-advanced.html[/#0000ff][/url]

[font "Calibri"]This is my search results page:
[/#000000]
[url "http://www.koshertravelinfo.com/kosher-hotels/kosher-hotel-listing.php"][font "Calibri"]http://www.koshertravelinfo.com/kosher-hotels/kosher-hotel-listing.php[/#0000ff][/url]

[font "Calibri"]Part of the search form has multiple check boxes. When selecting more than one checkbox, only the LAST selected type will show in the results. For example, selecting Large-scale Hotel and Vacation Home / Villa will only show Vacation Home / Villa types in the result. Please help…[/#000000]

Re: [RapidWeb] Search with checkbox only shows �last� selected item in search box

By Jason - January 30, 2012 - edited: January 30, 2012

Hi,

This is happening because they all have the same name (establishment). Selecting one value overwrites the previous value in the array. If you want to be able to select multiple values, you need to make establishment an array by adding [] to the name. For example:


<input type="checkbox" value="Large-scale Hotel" name="establishment[]" />

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] Search with checkbox only shows last selected item in search box

By (Deleted User) - January 30, 2012 - edited: January 30, 2012

Thank you for your prompt reply. I added the [] brackets as instructed (to allow multiple selections) but it does not work. In fact, it now completely ignores "Establishment". It ignores this filter. Please help.

Re: [RapidWeb] Search with checkbox only shows �last� selected item in search box

By (Deleted User) - January 30, 2012 - edited: January 30, 2012

Hi RapidWeb,

Adding square brackets to the end of a name for an input adds each value under that name to an array in POST.

When retrieving the data, you have to read it as an array as well, so instead of
$value = $_REQUEST['name']
you would use
foreach ( $_REQUEST['name'] as $value ) {
echo "I found $value!";
}


Hope this helps,

Tom

Re: [Tom P] Search with checkbox only shows last selected item in search box

By (Deleted User) - January 30, 2012

Thank you for your help. I am not a programmer. Can you give it to me in simple format? Below is part of my search form:

<input type="checkbox" value="Large-scale Hotel" name="establishment[]" />Large-scale Hotel<br>

<input type="checkbox" value="Small-scale Hotel" name="establishment[]" />Small-scale Hotel<br>

<input type="checkbox" value="Motel" name="establishment[]" />Motel<br>

<input type="checkbox" value="Country Inn" name="establishment[]" />Country Inn<br>

<input type="checkbox" value="Bed &amp; Breakfast" name="establishment[]" />Bed &amp; Breakfast<br>

<input type="checkbox" value="Condominium" name="establishment[]" />Condominium<br>

<input type="checkbox" value="Cabin/Cottage" name="establishment[]" />Cabin/Cottage<br>

<input type="checkbox" value="Ranch" name="establishment[]" />Ranch<br>

<input type="checkbox" value="Vacation Home / Villa" name="establishment[]" />Vacation Home / Villa<br>

<input type="checkbox" value="Resort" name="establishment[]" />Resort<br>


What am I doing wrong? All other fields in this advanced form work fine.

Re: [RapidWeb] Search with checkbox only shows last selected item in search box

By (Deleted User) - February 1, 2012

Hi RapidWeb,

Since you're trying to create a search query for one field that will look for several keywords you'll need to combine the data from the array and output it as a string before the search is performed.

All this happens before the records are loaded by CMSB (normally right at the start of a page).

To create a "where" statement that will search for each checked keyword in the establishment field, try the following:

$selected_establishments_where_statement = '';

if ( @$_REQUEST['establishments'] ) {

$selected_establishments_where_statement = '(';

foreach ( $_REQUEST['establishments'] as $establishment ) {

$selected_establishments_where_statement.= "establishment='".$establishment."' OR ";

}

$selected_establishments_where_statement = substr( $selected_establishments_where_statement,0,-4).")";

}


The above will produce a string along the lines of "(establishment='Hotel' OR establishment='Shop' OR establishment='Shop')" which you can drop into the getRecords function at the start of the page:
list($hotelsRecords,$hotelsMetaData) = getRecord(array(
'tableName' =>**TABLE**,
'where' => $selected_establishments_where_statement,
));


The above is an example, you may have other options set in your call.

Hope this helps,

Tom