0

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.

1 Answer 1

2

Try this :

<option value="<?php echo $line['contractors'];?>" <?php if(in_array($line['contractors'],$contractors)){?>checked="checked" <?php }?>> <?php echo $line['contractors'];?> </option>
Sign up to request clarification or add additional context in comments.

1 Comment

I change check to selected and it worked, thank you very much. I see what I was trying that went wrong. <option value="<?php echo $line['contractors'];?>" <?php if(in_array($line['contractors'],$contractors)){?>selected="selected" <?php }?>> <?php echo $line['contractors'];?> </option>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.