Hi,
What you have done there looks good, and I do not really see a lot of wiggle room for improvement. However, as you have multiple dropdowns, I would put this all in a function, which you can then use to create the dropdown. I have reworked your code a little, and made a function. No server here, so I cannot test it. Also, I am not sure whether the MySQLi calls work the same as the mysql_query functions. But I am sure someone can patch that up where needed. It should give you a starting point in any case. Naturally, you may want to clean it up a little and add some error trapping to it. But this would be the basis of a function I would use:
// Use like:
$query = "SELECT ward, ward_id FROM ward";
$KeyColum = "ward_id";
$LabelColum = "ward";
$sel = 1;
$warddropdown = createDropDown($query, $KeyColum, $LabelColum, $sel);
if($warddropdown <> -1)
{
echo $warddropdown;
}
else
{
echo "Sorry, no records found";
}
function createDropDown($sql,$idcol, $labelcol, $selected)
$r = mysqli_query($sql) or trigger_error("Query: $sql\n<br />MySQL Error: " . mysqli_error());
// If no records found, return -1 as an indicator of no records. In the main script handle output to user
if(mysqli_affected_rows($dbc) == 0)
{
return -1;
}
$drop = "<select>"
// loop through data and fill drop downs
while($rows=mysqli_fetch_array($r))
{
$sel = "";
if($rows[$idlcol] == $selected) // Set $sel to 'selected' if ID matches selected ID
{
$sel = " selected ";
}
$drop .= "<option value=\"".$rows[$idlcol]."\" ".$sel.">\"".$rows[$labelcol]."\"</option>
";
}
$drop .= "</select>";
return($drop);
}