PaperBagged
PaperBagged

Reputation: 177

Displaying a list of checkboxes based on an array

I have a list of checkboxes displayed below. This shows all the contactors and allows them to be selected via check box.

<?php
 $query = "SELECT * FROM form_4 GROUP BY contractors ASC";
$result = mysql_query($query);
?>
<li><select multiple="multiple" size="10" name="contractors[]">
        <option value="None Yet" selected="selected">None Yet
</option>
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['contractors'];?>"> <?php echo $line['contractors'];?> </option>
<?php
}
?>
            </select></li>

I have an array saved in another place that I would like to generate the list above but with the items in the array below already checked/selected.

<?php
$options = unserialize('contractors');
$result = mysql_query("SELECT * FROM form_2 WHERE jobname = 'testjob' GROUP BY jobname ORDER BY biddate ASC LIMIT 0, 1");

while($row = mysql_fetch_array($result))
  {
    $contractors = unserialize($row['contractors']);
  foreach ($contractors as $contractor)
  echo "" . htmlspecialchars ($contractor).' - ';
?>

Any help would be greatly appreciated.

Upvotes: 0

Views: 387

Answers (1)

Prasanth Bendra
Prasanth Bendra

Reputation: 32740

Try this :

<option value="<?php echo $line['contractors'];?>" <?php if(in_array($line['contractors'],$contractors)){?>checked="checked" <?php }?>> <?php echo $line['contractors'];?> </option>

Upvotes: 2

Related Questions