I'm trying to display a list of hospitals/insurance companies from which to choose, but need to include that list ten times in the same form. It would be ideal to have the second and subsequent lists of hospitals display only those hospitals which have not been previously selected -- a diminishing list, if you will. I'd just like to conquer the multiple lists first. Here's my current code:
$sqlhosp = "SELECT HospInsCo FROM hospitals ORDER BY HospInsCo ASC";
$sqlhosp_result = mysql_query($sqlhosp) or die (mysql_error());
// (snip)
<td width=\"200\"><select name=\"hospital1\">
<option value=\"\"> -- Select -- </option>";
while ($row = mysql_fetch_array($sqlhosp_result)) {
$HospInsCo = $row["HospInsCo"];
echo "<option value=\"$HospInsCo\">$HospInsCo</option>";
}
echo "
</select></td>
I've tried duplicating the above but changing the select name to hospital2, hospital3 and so on. Currently I only get the complete list of hospitals in the first select statement. Subsequent select statements only offer the option of " -- Select --". What is the best way to accomplish my goal?
I would guess you're only re-using the code to display the hospitals and not re-running the query. I suggest you load the results into an array and use that array to display results on each subsequent go-around.
I've toyed with that idea, but I don't think I understand arrays completely and how to really manipulate them. I gleaned the current code from examples found elsewhere. It appears the hospitals are in $row["HospInsCo"] and I've searched and tried different things like:
The only reason why I suggest you may need to run the query again is because you're popping elements off the result set until there is nothing left to pop in your first while loop. What happens then, later, if you try to loop through the results again? This is the loop popping elements off your result set:
$sqlhosp = "SELECT HospInsCo FROM hospitals ORDER BY HospInsCo ASC";
$sqlhosp_result = mysql_query($sqlhosp) or die (mysql_error());
$a = array();
while( $row = mysql_fetch_assoc($sqlhosp_result) )
$a[] = $row["HospInsCo"];
// loop through results any time in your code later...
foreach($a as $HospInsCo)
echo "<option value=\"$HospInsCo\">$HospInsCo</option>";