0

I'm current'y having 2 variables, for $arrAns, it contains the different answer that the user select for a checkbox. Example for $arrAns will be 1,2,3. And for $arr, it's the option variable which contains all the options that the question have. Example of $arr will be 1,2,3,4,5,6.

Here's the code whereby I trying to compare, if $arrAns == $arr, then the checkbox input will be "checked". else, it will be leave as blank.

But when I tried using the codes, if user's selection is 1,2,3. It works. But if user selects 2,3,4 non of the options will be "checked". And if user selects 1,3,4 only option 1 will be "checked".

Is there something wrong with the logic in between? In need of help, Thank you!

<?php if ($arrAns[$i] == $arr) { 
 ?>
    <input type="checkbox" name="<?php echo 'qns' . $qID; ?>[]" value="<?php echo $arr; ?>" class="required" checked/> <?php echo $arr; ?><br/>
<?php } else { ?>
    <input type="checkbox" name="<?php echo 'qns' . $qID; ?>[]" value="<?php echo $arr; ?>" class="required"/> <?php echo $arr; ?><br/>
    <?php
} ?>
4

2 回答 2

1

而不是查看元素是否存在,我认为您想检查它是否在数组或结果中。

改变

  <?php if ($arrAns[$i] == $arr) { 

  <?php if (in_array($arr, $arrAns)) { 
于 2013-06-17T04:31:06.843 回答
0

You can make an effective use of in_array():

in_array($arr, $arrAns)
于 2013-06-17T04:31:51.530 回答