Creating checkbox list and populating from MySQL

Ok, so I’ve spent quite a bit of time piecing together this solution from a variety of sources. As such, I may have something in my code below that doesn’t make sense or isn’t neccessary. Please let me know if that is the case.

I’m creating an administrative form that users will you to add/remove items from a MySQL table that lists open positions for a facility. The foreach loop generates all of the possible job specialties from a table called ‘specialty_list’. This table is joined to a second table (‘open_positions’) that lists any positions that have been selected previously.

Where I’m stuck is getting the checkbox to be checked if the facility_ID from the open_positions table matches the $id passed in the URL via ?facility_id=’’. Here’s where I am so far:

[php]
$query = “SELECT specialty_list.specialty_displayname
, specialty_shortname
, open_positions.position
, facility_ID
FROM specialty_list
LEFT OUTER
JOIN open_positions
ON open_positions.position = specialty_list.specialty_shortname
ORDER
BY specialty_list.specialty_shortname”;

$results = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($results))
{
$positions[$row[‘specialty_shortname’]] = $row[‘specialty_displayname’];
}

echo “”;
foreach($positions as $specialty_shortname => $specialty_displayname)
{
$facility_ID = $row[‘facility_ID’];

$checked = $facility_ID == $row[‘facility_ID’] ? ’ checked’ : ‘’;

echo “<input type=‘checkbox’ name=‘position[]’ value=”{$specialty_shortname}"{$checked}>  {$specialty_displayname}
";

}
echo “”;
echo “”;
echo “”;
[/php]

Any ideas how to get this working? I feel like I’m very close, but I just can’t get it. I also tried starting from scratch with a WHILE statement instead of a FOREACH, but haven’t tweaked it enough to prevent duplicate checkboxes. With that in mind, here it is, just in case that’s a better direction:

[php]
$query = “SELECT specialty_list.specialty_displayname
, specialty_shortname
, open_positions.position
, facility_ID
FROM specialty_list
LEFT OUTER
JOIN open_positions
ON open_positions.position = specialty_list.specialty_shortname
ORDER
BY specialty_list.specialty_shortname”;

$results = mysql_query($query) or die(mysql_error());

echo “”;

while($row=mysql_fetch_assoc($results))
{
$facility_ID = $row[‘facility_ID’];
$specialty_shortname = $row[‘specialty_shortname’];
$specialty_displayname = $row[‘specialty_displayname’];

if ($facililty_ID==$id) {
print $facility_ID." - “.$specialty_shortname.” - ".$specialty_displayname;
$checked=’ checked’;
}

echo “<input type=‘checkbox’ name=‘position[]’ value=”$specialty_shortname"$checked>  $specialty_displayname
";
}

echo “”;
echo “”;
echo “”;

[/php]

Probably you have already solved this, but in case if not - the line need to be updated in the first version of your code:
[php]$checked = $facility_ID == $row[‘facility_ID’] ? ’ checked’ : ‘’;[/php]

correct code is:
[php]$checked = $facility_ID == $_REQUEST[‘facility_ID’] ? ’ checked’ : ‘’;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service