Jump to content

n00bie needs help >> Multiple select list > search problems


sford999

Recommended Posts

I have this code

 

<form id="clubs_search" name="clubs_search" method="post" action="search.php?p=clubs">
<table width="100%" border="1">
  <tr>
    <td colspan="2"><div align="center">
      <h3>Search Angling Clubs</h3>
    </div></td>
  </tr>
  <tr>
    <td width="50%">Club Name </td>
    <td width="50%"><input name="club_name" type="text" id="club_name" /></td>
  </tr>
  <tr>
    <td width="50%">Town</td>
    <td width="50%"><input name="town" type="text" id="town" /></td>
  </tr>
  <tr>
    <td width="50%">County</td>
    <td width="50%"><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>
  </tr>
  <tr>
    <td width="50%">Postcode</td>
    <td width="50%"><input name="postcode" type="text" id="postcode" /></td>
  </tr>
  <tr>
    <td width="50%">Price (low > high) </td>
    <td width="50%">£
      <input name="price_low" type="text" value="0.00" size="8" maxlength="6" />
      To £
      <input name="price_high" type="text" value="0.00" size="8" maxlength="6" /></td>
  </tr>
  <tr>
    <td width="50%">Disabled Access</td>
    <td width="50%"><input name="disabled" type="radio" value="1" />
      Yes 
      <input name="disabled" type="radio" value="0" />
      No</td>
  </tr>
  <tr>
    <td width="50%">Carp Syndicate </td>
    <td width="50%"><input name="carp" type="radio" value="1" />
      Yes 
      <input name="carp" type="radio" value="0" />
      No</td>
  </tr>
  <tr>
    <td width="50%">Night Fishing Allowed </td>
    <td width="50%"><input name="night_fishing" type="radio" value="1" />
Yes
  <input name="night_fishing" type="radio" value="0" />
No</td>
  </tr>
  <tr>
    <td width="50%">Enforced Closed Season </td>
    <td width="50%"><input name="closed_season" type="radio" value="1" />
Yes
  <input name="closed_season" type="radio" value="0" />
No</td>
  </tr>
  <tr>
    <td width="50%">Open For Membership  </td>
    <td width="50%"><input name="opm" type="radio" value="1" />
Yes
  <input name="opm" type="radio" value="0" />
No</td>
  </tr>
   <tr>
    <td width="50%">Species Available </td>
    <td width="50%">
    <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>
    </select></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
      <input type="reset" name="Reset" value="Reset Form" />  <input type="Submit" name="Submit" value="Start Search" />      
    </div></td>
  </tr>
</table>
</form>

 

The problem I`m having is passing the species[] form list.

The items are passing to the processor ok, but whenever I echo $_POST['species']; it shows as "Array" and not as the defined number/s

 

I was able to extract the array using

 

<?php

foreach ($_POST['species'] as $key=>$value) 
{
         if ($_POST['species'][$key]) 
        {
         echo $value;
         echo ", ";
}
}

?>

 

That shows it as 1, 2, 3, 4, 5,  and I`m clueless in how I`d get this to work in an SQL query.

 

The current code I have for building the query is this:

 

<?php

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%'"; 
}

$whereClause = implode(' OR ', $whereParts);
$sql = "SELECT * FROM fisheries WHERE " . $whereClause;

?>

 

How would I add the multiple select to the above so that its searchable.

 

Thanks in Advance :)

<select name="species[]" size="5" multiple="multiple">

Try this:

<select name="species" size="5" multiple="multiple">

I'm sure those square brackets shouldn't be there ;)

 

I think it does as its a multiple search field, if it isn`t there it will only show the last selected item, instead of all selected items.

Its not only at the moment as I'm coding it on my localhost server.

 

Heres the complete code with the parts commented out which are not needed to run the script.

 

<?php

//include('functions.php');

if(isset($_POST['Submit']))
{
$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']);
$species = mysql_real_escape_string($_POST['species']);

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%'";
}

foreach ($_POST['species'] as $key=>$value)
{
	if ($_POST['species'][$key])
	{
		echo $value;
	}
}
$whereClause = implode(' OR ', $whereParts);
$sql = "SELECT * FROM clubs WHERE " . $whereClause;

// Haven't added the mysql_query function yet,
// I wanted to get things passing correctly before adding that

//show_header($title = "Search Angling Clubs » Results");
echo "Clubs Results";
echo "<br />";
echo $value;
echo "<br />";
echo $sql;
//show_footer();
exit();
}
else
{
//show_header($title = "Search Angling Clubs");
echo "<form id=\"clubs_search\" name=\"clubs_search\" method=\"post\" action=\"search.php\">
<table width=\"100%\" border=\"1\">
  <tr>
    <td colspan=\"2\"><div align=\"center\">
      <h3>Search Angling Clubs</h3>
    </div></td>
  </tr>
  <tr>
    <td width=\"50%\">Club Name </td>
    <td width=\"50%\">
    <input name=\"club_name\" type=\"text\" id=\"club_name\" />
    </td>
  </tr>
  <tr>
    <td width=\"50%\">Town</td>
    <td width=\"50%\">
    <input name=\"town\" type=\"text\" id=\"town\" />
    </td>
  </tr>
  <tr>
    <td width=\"50%\">County</td>
    <td width=\"50%\">
    <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>
  </tr>
  <tr>
    <td width=\"50%\">Postcode</td>
    <td width=\"50%\">
    <input name=\"postcode\" type=\"text\" id=\"postcode\" />
    </td>
  </tr>
  <tr>
    <td width=\"50%\">Price (low > high)</td>
    <td width=\"50%\">
    £<input name=\"price_low\" type=\"text\" value=\"0.00\" size=\"8\" maxlength=\"6\" />
    To
    £<input name=\"price_high\" type=\"text\" value=\"0.00\" size=\"8\" maxlength=\"6\" />
    </td>
  </tr>
  <tr>
    <td width=\"50%\">Disabled Access</td>
    <td width=\"50%\">
    <input name=\"disabled\" type=\"radio\" value=\"1\" />Yes 
    <input name=\"disabled\" type=\"radio\" value=\"0\" />No
    </td>
  </tr>
  <tr>
    <td width=\"50%\">Carp Syndicate</td>
    <td width=\"50%\">
    <input name=\"carp\" type=\"radio\" value=\"1\" />Yes 
    <input name=\"carp\" type=\"radio\" value=\"0\" />No
    </td>
  </tr>
  <tr>
    <td width=\"50%\">Night Fishing Allowed</td>
    <td width=\"50%\">
    <input name=\"night_fishing\" type=\"radio\" value=\"1\" />Yes
    <input name=\"night_fishing\" type=\"radio\" value=\"0\" />No
    </td>
  </tr>
  <tr>
    <td width=\"50%\">Enforced Closed Season</td>
    <td width=\"50%\">
    <input name=\"closed_season\" type=\"radio\" value=\"1\" />Yes
    <input name=\"closed_season\" type=\"radio\" value=\"0\" />No
    </td>
  </tr>
  <tr>
    <td width=\"50%\">Open For Membership</td>
    <td width=\"50%\">
    <input name=\"opm\" type=\"radio\" value=\"1\" />Yes
    <input name=\"opm\" type=\"radio\" value=\"0\" />No
    </td>
  </tr>
   <tr>
    <td width=\"50%\">Species Available </td>
    <td width=\"50%\">
    <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>
    </select>
    </td>
  </tr>
  <tr>
    <td colspan=\"2\"><div align=\"center\">
      <input type=\"reset\" name=\"Reset\" value=\"Reset Form\" />
        
      <input type=\"Submit\" name=\"Submit\" value=\"Start Search\" />      
    </div></td>
  </tr>
</table>
</form>";
//show_footer();
}


?>

I see a huge error..you keep setting whereParts[] to something different. It is just going to keep over-writing itself, but I also see you don't even have an index for the array whereParts selected so it should be having tons of errors.

$whereParts[] is being set by the posted sections of the form, if nothing is submitted, then it will ignore the current section and move onto the next, and so on and so on until it reaches the end and then

 

$whereClause = implode(' AND ', $whereParts);
$sql = "SELECT * FROM clubs WHERE ". $whereClause;

 

Will build the MySQL query, which will look something like this

 

SELECT * FROM fisheries WHERE club_name LIKE '%test%' AND town LIKE '%test%' AND 
county = '%1%' AND postcode LIKE '%test%' AND price_low LIKE '%0.00%' AND price_high LIKE '%0.00%' 
AND disabled_access = '%1%' AND carp_syndicate = '%1%' AND night_fishing_allowed = '%1%' 
AND close_season = '%1%' AND open_membership = '%1%

 

I have error_reporting(E_ALL); on and its not producing any errors whatsoever

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.