I'm working with a table in Bootstrap and one of the columns is for a checkbox. The table is filled with the values from a mysql db. In the db the column for the checkbox has a value of 1, or 0. I want the checkbox to be checked if the value is 1. I have this code, but it only works for the first checkbox. How can i use it for all checkboxes? This is my code.
<tbody>
<?php while($row = $sql->fetch()) { ?>
<tr>
<td><?php echo $row['Opleiding_ID']; ?></td>
<td><?php echo $row['Opleiding']; ?></td>
<?php $checked = $row['Hidden']; ?>
<script type="text/JavaScript">
$(document).ready(function(){
var arr<?php echo $row["Opleiding_ID"]; ?> = <?php echo json_encode($checked); ?>;
if (arr<?php echo $row["Opleiding_ID"]; ?> == "1") {
$("#mycheckbox<?php echo $row["Opleiding_ID"]; ?>").prop('checked', true);
} else {
$("#mycheckbox<?php echo $row["Opleiding_ID"]; ?>").prop('checked', false);
}
});
</script>
<td><input type="checkbox" id="mycheckbox<?php echo $row["Opleiding_ID"]; ?>" name="checks" value="" class="check"></td>
</tr>
<?php } ?>
</tbody>
mycheckboxand generally ID fields should be unique. If we see what your markup looks like, it might make this a bit easier.