Currently, I have a column in database called answer to store all the answers for the form submitted. For now, the form itself have a "save" button whereby user can save the form if they're not free to finish it at the point of time. When the form is being saved, all the answers will be inserted into database.
Now my question is, my checkbox input answers are being inserted as example "1,2,3" if the question itself have option of "1,2,3,4,5". so I used a foreach loop to separate the data out and use if/else statement to check if it's same, marked as tick. But the question format end up to be like
x 1
2
x 2
3
3
x 3
4
4
4
5
5
5
The output that I want is:
x 1
x 2
x 3
4
5
The code for the printing of checkbox is like this:
First foreach is the options of the question, example "1,2,3,4,5".
Second foreach is the answers that the user selected after saving the form, example "1,2,3".
foreach ($arr as $row => $name) {
$arr = $name;
if ($input == 'Multiple choice (multiple answers) [Check box]') {
foreach ($arrAns as $values) {
if ($arr == $values) { ?>
<input type="checkbox" name="<?php echo 'qns' . $qID; ?>[]" value="<?php echo $arr; ?>" class="required" checked/> <?php echo $arr; ?><br/>
<?php break; } else { ?>
<input type="checkbox" name="<?php echo 'qns' . $qID; ?>[]" value="<?php echo $arr; ?>" class="required"/> <?php echo $arr; ?><br/>
<?php
} } } } ?>
Is it possible to get the output that I want? Thanks for the help!
checked
attribute should be handled in the inner foreach ( besides useif (in_array('foo', $array))
for that inner one ). And I really dislike$input == 'Multiple choice (multiple answers) [Check box]'
as an if statement. Please split displayed text and logic content for your own safety. – Najzero Jun 14 at 8:15$input
and why are you checking it inside yourforeach
(i.e., at the start of every iteration of the loop) if its value is never changed during the course of the loop? – daiscog Jun 14 at 9:24