When a user is editing data in a form that is populated from a database, I am stuggling to get checkboxes pre-selected, I am working with 2 arrays, the first is the topics available,
Array ( [0] => Array (
[topic_id] => 402
[topic_title] => Website Development
[topic_slug] => website-development
[topic_description] => This topic will cover everything form the most CSS to the most advanced PHP. You will learn not only how to code but create maintable code bases that can be authored by various authors with no previous knowledge of the project.
[date_created] => 2011-10-03 17:27:41
)
[1] => Array (
[topic_id] => 404
[topic_title] => j41-ramp-handling
[topic_slug] => j41-ramp-handling
[topic_description] => Ramp handling course for J41
[date_created] => 2011-11-02 23:14:00
)
[2] => Array (
[topic_id] => 405
[topic_title] => aviation-regulations
[topic_slug] =>
[topic_description] => Changes to aviation regulations.
[date_created] => 2011-10-17 10:19:40 ) )
and I also have an array showing what topics the user is signed up for,
Array ( [0] => Array (
[topic_title] => Website Development
[topic_id] => 402
[topic_slug] => website-development
[topic_description] => This topic will cover everything form the most CSS to the most advanced PHP. You will learn not only how to code but create maintable code bases that can be authored by various authors with no previous knowledge of the project.
)
[1] => Array (
[topic_title] => aviation-regulations
[topic_id] => 405
[topic_slug] =>
[topic_description] => Changes to aviation regulations.
))
The first array is called $topics the second is called $signedup I am trying to check the checkbox by default if the ttopic id matches in both array, however, whatever I try I get not tick, below is my HTML/PHP code,
<fieldset>
<legend>Topics Sign Up</legend>
<?php $i = 0; ?>
<?php foreach ($topics as $k => $v) : ?>
<?php //var_dump($signedup[$i]['topics_topic_id']); ?>
<label for="topics_topic_id[]" class="checkbox"><?php echo $v['topic_title'];?></label>
<input type="checkbox" name="topics_topic_id[]" value="<?php echo $v['topic_id']; ?>"/>
<?php $i++; ?>
<?php endforeach; ?>
</fieldset>
How can I check the checkbox by default if the signed up array has a topic id in it that match that of the topic aray?
You could store an array of the topic ids from
$signeduplike soThen as you loop through
$topics, check whether the id is in$signedUpIds, if so, addingchecked="checked"to the checkbox attributes