Jump to content

Creating checkbox list and populating from multiple MySQL tables


BlackAce

Recommended Posts

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:

 

$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 "<form method='POST' action='checkbox.php'>";
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}</input><br/>"; 

}  
echo "<input type='hidden' name='facility_ID' value='$id'>";
echo "<input type='submit' value='Submit Checkboxes!'>";
echo "</form>";

 

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:

 

$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 "<form method='POST' action='checkbox.php'>";

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) {

$checked=' checked';
}

echo "<input type='checkbox' name='position[]' value=\"$specialty_shortname\"$checked>  $specialty_displayname</input><br/>"; 
}

echo "<input type='hidden' name='facility_ID' value='$id'>";
echo "<input type='submit' value='Submit Checkboxes!'>";
echo "</form>";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.