My question extends on the answer on this post and I couldn’t find what I’m looking for posted otherwise.
How do I auto select (checked) options in a checkbox list when the options are being pulled from a mySQL table (dynamic)?
Here’s what I have so far:
Pulling the committees (comms) from the table:
while ($row = mysql_fetch_assoc($comm)){
foreach ($row as $v){
$comms[] = $v;
}
}
outputting pretty HTML with tidy PHP (the part I need help with):
foreach ($comms as $comm){
?????????
}
How can I output something like?:
<input type="checkbox" name="committee" value="blue" checked="checked" />blue<br />
<input type="checkbox" name="committee" value="green" />green<br />
<input type="checkbox" name="committee" value="orange" />orange<br />
<input type="checkbox" name="committee" value="purple" checked="checked" />purple<br />
Assuming your colors are stored in an array and your data comes back as an array (likely not the case if you’re using
mysql_*functions), you can print out each checkbox, then compare its value to see if it’s in the list of “checked” colors:By the way, you should stop using
mysql_*functions. They’re being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you’re not sure which one to use, read this article.