View Full Version : Multiple select list > search problems

02-25-2007, 05:59 PM
I`m building a search form which has a multiple select list, but I can`t figure out how to search for the items in the array that the list creates.

The form is below:

echo "<form id=\"clubs_search\" name=\"clubs_search\" method=\"post\" action=\"".$_SERVER['PHP_SELF']."?p=clubs\">
<table width=\"100%\" border=\"0\" class=\"tablea\">
<td colspan=\"2\" class=\"tablec\"><div align=\"center\">
<h3>Search Angling Clubs</h3>
<td width=\"50%\" class=\"tablec\">Club Name </td>
<td width=\"50%\" class=\"tablec\"><input name=\"club_name\" type=\"text\" id=\"club_name\" /></td>
<td width=\"50%\" class=\"tablec\">Town</td>
<td width=\"50%\" class=\"tablec\"><input name=\"town\" type=\"text\" id=\"town\" /></td>
<td width=\"50%\" class=\"tablec\">County</td>
<td width=\"50%\" class=\"tablec\"><select name=\"county\" class=\"textfield\">
<option value=\"1\" selected=\"selected\">Avon</option>
<option value=\"2\">Bedfordshire</option>
<option value=\"3\">Berkshire</option>
<option value=\"4\">Buckinghamshire</option>
<option value=\"5\">Cambridgeshire</option>
<option value=\"6\">Cheshire</option>
<option value=\"7\">Cleveland</option>
<option value=\"8\">Cornwall</option>
<option value=\"9\">Cumbria</option>
<option value=\"10\">Derbyshire</option>
<option value=\"11\">Devon</option>
<option value=\"12\">Dorset</option>
<option value=\"13\">Durham</option>
<option value=\"14\">East Sussex</option>
<option value=\"15\">Essex</option>
<option value=\"16\">Gloucestershire</option>
<option value=\"17\">Gt London</option>
<option value=\"18\">Gt Manchester</option>
<option value=\"19\">Hampshire</option>
<option value=\"20\">Hereford and Worcester</option>
<option value=\"21\">Hertfordshire</option>
<option value=\"22\">Humberside</option>
<option value=\"23\">Kent</option>
<option value=\"24\">Lancashire</option>
<option value=\"25\">Leicestershire</option>
<option value=\"26\">Lincolnshire</option>
<option value=\"27\">Merseyside</option>
<option value=\"28\">Norfolk</option>
<option value=\"29\">Northamptonshire</option>
<option value=\"30\">Northumberland</option>
<option value=\"31\">Nottinghamshire</option>
<option value=\"32\">Oxfordshire</option>
<option value=\"33\">Scotland</option>
<option value=\"34\">Shropshire</option>
<option value=\"35\">Somerset</option>
<option value=\"36\">Staffordshire</option>
<option value=\"37\">Suffolk</option>
<option value=\"38\">Surrey</option>
<option value=\"39\">Tyne and wear</option>
<option value=\"40\">Wales</option>
<option value=\"41\">Warwickshire</option>
<option value=\"42\">West Sussex</option>
<option value=\"43\">Wiltshire</option>
<option value=\"44\">Yorkshire</option>
</select> </td>
<td width=\"50%\" class=\"tablec\">Postcode</td>
<td width=\"50%\" class=\"tablec\"><input name=\"postcode\" type=\"text\" id=\"postcode\" /></td>
<td width=\"50%\" class=\"tablec\">Price (low &gt; high) </td>
<td width=\"50%\" class=\"tablec\">&pound;
<input name=\"price_low\" type=\"text\" size=\"8\" maxlength=\"6\" />
To &pound;
<input name=\"price_high\" type=\"text\" size=\"8\" maxlength=\"6\" /></td>
<td width=\"50%\" class=\"tablec\">Disabled Access</td>
<td width=\"50%\" class=\"tablec\"><input name=\"disabled\" type=\"radio\" value=\"1\" />
<input name=\"disabled\" type=\"radio\" value=\"0\" />
<td width=\"50%\" class=\"tablec\">Carp Syndicate</td>
<td width=\"50%\" class=\"tablec\"><input name=\"carp\" type=\"radio\" value=\"1\" />
<input name=\"carp\" type=\"radio\" value=\"0\" />
<td width=\"50%\" class=\"tablec\">Night Fishing Allowed</td>
<td width=\"50%\" class=\"tablec\"><input name=\"night_fishing\" type=\"radio\" value=\"1\" />
<input name=\"night_fishing\" type=\"radio\" value=\"0\" />
<td width=\"50%\" class=\"tablec\">Enforced Closed Season</td>
<td width=\"50%\" class=\"tablec\"><input name=\"closed_season\" type=\"radio\" value=\"1\" />
<input name=\"closed_season\" type=\"radio\" value=\"0\" />
<td width=\"50%\" class=\"tablec\">Open For Membership </td>
<td width=\"50%\" class=\"tablec\"><input name=\"opm\" type=\"radio\" value=\"1\" />
<input name=\"opm\" type=\"radio\" value=\"0\" />
<td width=\"50%\" class=\"tablec\">Species Available </td>
<td width=\"50%\" class=\"tablec\">
<select name=\"species\" size=\"5\" multiple=\"multiple\">
<option value=\"1\">Barbel</option>
<option value=\"2\">Bleak</option>
<option value=\"3\">Bream</option>
<option value=\"4\">Carp</option>
<option value=\"5\">Carp (Crucian)</option>
<option value=\"6\">Carp (Grass)</option>
<option value=\"7\">Catfish</option>
<option value=\"8\">Chub</option>
<option value=\"9\">Dace</option>
<option value=\"10\">Eel</option>
<option value=\"11\">Grayling</option>
<option value=\"12\">Gudgeon</option>
<option value=\"13\">Minnow</option>
<option value=\"14\">Orfe</option>
<option value=\"15\">Perch</option>
<option value=\"16\">Pike</option>
<option value=\"17\">Roach</option>
<option value=\"18\">Rudd</option>
<option value=\"19\">Salmon</option>
<option value=\"20\">Sturgeon</option>
<option value=\"21\">Tench</option>
<option value=\"22\">Trout</option>
<option value=\"23\">Trout (Blue)</option>
<option value=\"24\">Trout (Brook)</option>
<option value=\"25\">Trout (Brown)</option>
<option value=\"26\">Trout (Rainbow)</option>
<option value=\"27\">Trout (Tiger)</option>
<option value=\"28\">Zander</option>

<td colspan=\"2\" class=\"tablec\"><div align=\"center\">
<input type=\"reset\" name=\"Reset\" value=\"Reset Form\" />&nbsp;&nbsp;<input type=\"Submit\" name=\"Submit\" value=\"Start Search\" />

The processing side of the script is:

$club_name = mysql_real_escape_string($_POST['club_name']);
$town = mysql_real_escape_string($_POST['town']);
$county = mysql_real_escape_string($_POST['county']);
$postcode = mysql_real_escape_string($_POST['postcode']);
$price_low = mysql_real_escape_string($_POST['price_low']);
$price_high = mysql_real_escape_string($_POST['price_high']);
$disabled = mysql_real_escape_string($_POST['disabled']);
$carp = mysql_real_escape_string($_POST['carp']);
$night_fishing = mysql_real_escape_string($_POST['night_fishing']);
$closed_season = mysql_real_escape_string($_POST['closed_season']);
$opm = mysql_real_escape_string($_POST['opm']);

if($club_name){ $whereParts[] = "club_name LIKE '%$club_name%'"; }
if($town){ $whereParts[] = "town LIKE '%$town%'"; }
if($county){ $whereParts[] = "county = '%$county%'"; }
if($postcode){ $whereParts[] = "postcode LIKE '%$postcode%'"; }
if($price_low){ $whereParts[] = "price_low LIKE '%$price_low%'"; }
if($price_high){ $whereParts[] = "price_high LIKE '%$price_high%'"; }
if($disabled){ $whereParts[] = "disabled_access = '%$disabled%'"; }
if($carp){ $whereParts[] = "carp_syndicate = '%$carp%'"; }
if($night_fishing){ $whereParts[] = "night_fishing_allowed = '%$night_fishing%'"; }
if($closed_season){ $whereParts[] = "close_season = '%$closed_season%'"; }
if($opm){ $whereParts[] = "open_membership = '%$opm%'"; }

if (isset($_POST['species']) && is_array($_POST['species']))
$search = array();
foreach($_POST['species'] as $cid)
if (intval($cid) != 0) array_push($search, intval($cid));
if (count($search) > 0)
$search = implode(',', $search_cid);
array_push($whereParts[], "species LIKE %$search%");
$whereClause = implode(' AND ', $whereParts);
$sql = "SELECT * FROM fisheries WHERE " . $whereClause;

echo "Results";
// Normally the SQL query would go here, but I want it to echo out the query before I build the rest of the script
echo "<br />";
echo $sql;


Does anyone have any ideas why this won`t work?

02-26-2007, 02:52 PM
Can u explain it a bit more what exactly is the problem.

02-26-2007, 03:57 PM
You need to name your select list name="county[]" rather than name="county". This will feed all selected counties into $_POST as an array, i.e. $_POST['county'][0] = the first county selected, $_POST['county'][1] = the 2nd, and so on.

Then you just need to alter your query to look for any value that matches the values selected. I would avoid the "LIKE %" logic for this and instead use the "IN()" logic.

$sql = 'SELECT *
FROM tablename
WHERE county in(';
for ($i = 0; $i < count($_POST['county']); $i++) {
if ($i != 0) {
$sql .= ', ';
$sql .= $_POST['county'][$i];
$sql .= ')';

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum