I am trying to create an admin form that allows users to select their requirements and save to a db using a many-to-many relationship.
I have been able to use a simple while loop to render the options for me:
while ($rows = $equipresults->fetch()) {
$eqid = $rows['req_id'];
$eqname = $rows['requirement'];
echo "
<label class='checkbox'>
<input type='checkbox' name='requirement[]' value='$eqid'> $eqname
</label><br>
";}
I am trying to mark the check boxes as checked if the user has previously checked the control and saved to the db. I have created the below array using a RIGHT JOIN between the equipment table matching an my artist ID or IS NULL.
Array (
[0] => Array ( [artistid] => 2 [req_id] => 1 [requirement] => Microphone )
[1] => Array ( [artistid] => [req_id] => 2 [requirement] => Table )
[2] => Array ( [artistid] => 2 [req_id] => 3 [requirement] => Chair )
[3] => Array ( [artistid] => [req_id] => 4 [requirement] => Microphone Stand )
[4] => Array ( [artistid] => [req_id] => 5 [requirement] => Personal Artifacts )
[5] => Array ( [artistid] => [req_id] => 6 [requirement] => Set Dressing )
[6] => Array ( [artistid] => [req_id] => 7 [requirement] => Raised Step )
[7] => Array ( [artistid] => [req_id] => 8 [requirement] => Other ) )
I have been playing about with a foreach loop, but I just can't manage to get the checkboxes to be checked.
$req=explode(",",$rows['requirement']);
if (!empty($equipresult)) {
foreach ($equipresult as $row) {
$checked = (in_array($row, $req)) ? 'checked="checked"' : '';
?>
<label class='checkbox'>
<input type="checkbox" name="requirement[]" value="<?php echo $row;?>"
<?php $checked;?>><?php echo implode(", ", $row);?>
</label><br>
<?php
}
}
?>
Ultimately I am trying to avoid hardcoding the html. Any guidance here would be really appreciated. I am also sure there is probably a really simple way of doing this
Aucun commentaire:
Enregistrer un commentaire