I am trying to query using the results of a search form. One of the fields I have is "state". I would like the user to be able to select multiple states and then get a list of results reflecting all the states selected. This is the code I have used for a single select form element for state. It reflects selecting all records if no selection is made for state, as well as sex, sale and salename:
$query = "SELECT *, DATE_FORMAT(saledate,'%m/%d/%Y') as saledatex FROM sdusa WHERE";
if (!empty($state)){
$query .= " state = '$state' AND";
}
if (!empty($sex)){
$query .= " sex = '$sex' AND";
}
if (!empty($sale)){
$query .= " breeder = '$sale' AND";
}
if (!empty($salename)){
$query .= " salename LIKE '%$salename%' AND";
}
$query .= " saledate >= $salestart
AND saledate <= $saleend
AND birthdate >= $birthstart
AND birthdate <= $birthend
AND actbw <= $actbw1
AND adj205ww >= $adj205ww1
AND adj365yw >= $adj365yw1
AND rea >= $rea1
AND imf >= $imf1
AND sc >= $sc1
$result = mysql_query($query) or die('Error, query failed');
How would I adjust the query to get results from a multiple select list for state that would give me a list of results that reflects all states selected? I think I need to add a "foreach" statement possibly or a counter - but since I am newer to php am not real sure of the code.
Current output looks like this:
<table align='center' width='780' border='1' cellpadding='0' cellspacing='0' bordercolor='#000000'>
<tr>
<td width='55' class='s11'><div align='center'>Reg #</div></td>
<td width='100' class='s11'><div align='center'>Name</div></td>
<td width='135' class='s11'><div align='center'>Sire</div></td>
<td width='125' class='s11'><div align='center'>MGS</div></td>
<td width='180' class='s11'><div align='center'>Breeder</div></td>
<td width='25' class='s11'><div align='center'>St</div></td>
<td width='60' class='s11'><div align='center'>Sale Date</div></td>
<td width='25' class='s11'><div align='center'>BW</div></td>
<td width='25' class='s11'><div align='center'>WW</div></td>
<td width='25' class='s11'><div align='center'>YW</div></td>
<td width='25' class='s11'><div align='center'>M</div></td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr height='20'>";
echo "<td class='s11'><div align='center'><a href='sdindselect.php?registration_num=". $row ['registration_num'] ."'>" . $row['registration_num'] . "</a> </div></td>";
echo "<td class='s11'><div align='center'>" . $row['name'] . " </div></td>";
echo "<td class='s11'><div align='center'>" . $row['sire'] . " </div></td>";
echo "<td class='s11'><div align='center'>" . $row['sire3'] . " </div></td>";
echo "<td class='s11'><div align='center'>" . $row['breeder'] . $row['salname'] ." </div></td>";
echo "<td class='s11'><div align='center'>" . $row['salestate'] . " </div></td>";
echo "<td class='s11'><div align='center'>" . $row['saledatex'] . " </div></td>";
echo "<td class='s11'><div align='center'>" . $row['bw'] . " </div></td>";
echo "<td class='s11'><div align='center'>" . $row['ww'] . " </div></td>";
echo "<td class='s11'><div align='center'>" . $row['yw'] . " </div></td>";
echo "<td class='s11'><div align='center'>" . $row['milk'] . " </div></td>";
echo "</tr>";
}
?>
Thanks for the help!
UPDATED! A multiple select list will pass its values as an array. What you'll need to do is something like this:
if (!empty($state))
{
$query .= "("; // Encapsulate state values so they'll be resolve independently
foreach($states as $term)
{
if($term !== '')
{
$query .= " state = '".trim($term)."' OR";
}
}
$query .= ") AND";
}
This should do it for you :
if (!empty($state)){
$states = array();
Depending on how each $state
value is sepertated in you query string
Exemple : $state = "New Jersey,Wyoming,Oregon,Georgia,Maine,Arkansas"
$states = explode(",",$state);
$first = true;
foreach($states as $term){
if($term !== ''){
if(!$first) $query .= " OR";
$query .= " state = '".trim($term)."'";
$first = false;
}
}
$query .= " AND";
}
Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.