Here's my php script, it pulls record from DB then it creates dynamic checkbox and the checkbox is a list of fruits that user needs to tick for selection.
What I want to achieve is to make the checkbox checked automatically if it matches some info from another array.
let say array contents are Banana, Mango, Apple. I want those checkbox to have checked automatically. how can i do that? thanks.
<?php
fruits = $stmt->prepare("SELECT * FROM fruits ORDER by fruit_id ASC");
$fruits->execute();
$cols = 6;
do {
echo "<tr>";
for ($i = 1; $i <= $cols; $i++) {
$row = $fruits->fetch(PDO::FETCH_ASSOC);
if ($row) {
$fruit_id = $row['fruit_id'];
$fruit_name = $row['fruit_name'];
?>
<td>
<table>
<tr valign="top">
<td>
<?php echo '<input type="checkbox" id="fruit_id[]" name="fruit_id[]" value="' . $fruit_id . '"/>' . $fruit_name . "\n"; ?>
</td>
<td width="30"> </td>
</tr>
</table>
</td>
<?php
} else {
echo "<td> </td>";
}
}
} while ($row);
?>