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!
center
in thes11
class, just think of all the bandwidth your save... – Lawrence Cherone Jul 25 '11 at 17:51