1

I have check box inside a foreach loop so that i want to checkbox checked.

<?php
    $heads = TCG\Voyager\Models\Head::all();
    $i=0;                                 
    foreach($heads as $heads){
        $headvalues=explode(',',$test->reporting_head);
        echo $headvalues[$i]; 
        echo '<br>';
        echo $heads->id;
?>
<input type="checkbox"  name="check_list[]" value="{{$heads->id}}_{{$heads->email}}" <?php echo (($headvalues[$i]==$heads->id)? 'checked' : ''); ?>><label>{{$heads->headname}}</label>
<?php 
        $i++;
    }
?>

Here echo $headvalues[$i] are 1, 2 ,3 and echo $heads->id are 1,3,2.so i want to checked all this check boxes but now only one check box is checked.i think my logic has issue.I tried the following

<?php echo (($headvalues[$i]==$heads->id)? 'checked' : ''); ?>

Please help me

4
  • What is the issue you are facing ? Commented Mar 27, 2017 at 5:26
  • @MayankPandeyz-i updated the question.please help me Commented Mar 27, 2017 at 5:49
  • What is the issue you are facing ? Commented Mar 27, 2017 at 5:54
  • actually ids 1 3 2 should checked but only first seems checked others are not. Commented Mar 27, 2017 at 6:03

1 Answer 1

1

Instead of

<?php echo (($headvalues[$i]==$heads->id)? 'checked' : ''); ?>

use

<?php echo (in_array($heads->id, $headvalues)? 'checked' : ''); ?>
Sign up to request clarification or add additional context in comments.

1 Comment

-thnks dear...Your so sweet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.