0

I'm print this two array in this forech loop. if i select last checkbox item after submit it will check first item didn't checked last item. that is my problem.

Here is my code example image Here is my code example image

Problem problem

I'm also try other question answer but didn't work for me.

  1. How to use multiple arrays in " single " foreach() loop

  2. Two arrays in foreach loop

  3. How to display two arrays in one foreach loop?

Thanks in advance.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
MD Iyasin Arafat
  • 763
  • 1
  • 12
  • 22

3 Answers3

1

I'm solved my problem using two php array function.

  1. array_column
  2. in_array

Here is my solved code:

    @elseif(isset($groupsData) && !$groupsData->isEmpty() or isset($qids) && !$qids->isEmpty())
    <?php
        $i = 0;
        $gids = array_column($qids->toArray(),'group_id');
    ?>
    @foreach($groupsData as $key => $data)
        <?php $i++;  ?>
        <tr>
            <td><label>
            <input type="checkbox" name="groups_id[]" value="{{ $data->id }}" class="check_checkbox" @if(in_array($data->id,$gids)) {{ 'checked' }} @endif >
            </label></td>
            <td>{{ $i }}</td>
            <td>{{ $data->group_name }}</td>
        </tr>
    @endforeach
@else

Screenshot:

enter image description here

MD Iyasin Arafat
  • 763
  • 1
  • 12
  • 22
0

My blade code:

@elseif(isset($groupsData) && !$groupsData->isEmpty() or isset($qids) && !$qids->isEmpty())
<?php $i = 0; ?>
@foreach($groupsData as $key => $data)
    <?php $i++; ?>
    <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="{{ $data->id }}" class="check_checkbox" @if(isset($qids[$key]->group_id)) {{ 'checked' }} @endif ></label></td>
        <td>{{ $i }}</td>
        <td>{{ $data->group_name }}</td>
    </tr>
@endforeach @else

This is print_r code:

Illuminate\Database\Eloquent\Collection Object

( [items:protected] => Array ( [0] => App\QuizGroupQuestionId Object ( [fillable:protected] => Array ( [0] => quiz_id [1] => group_id [2] => question_id )

                [dates:protected] => Array
                    (
                        [0] => deleted_at
                    )

                [connection:protected] => mysql
                [table:protected] => 
                [primaryKey:protected] => id
                [keyType:protected] => int
                [incrementing] => 1
                [with:protected] => Array
                    (
                    )

                [withCount:protected] => Array
                    (
                    )

                [perPage:protected] => 15
                [exists] => 1
                [wasRecentlyCreated] => 
                [attributes:protected] => Array
                    (
                        [id] => 12
                        [quiz_id] => 1
                        [group_id] => 8
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 12
                        [quiz_id] => 1
                        [group_id] => 8
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [casts:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [appends:protected] => Array
                    (
                    )

                [events:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [relations:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [timestamps] => 1
                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [forceDeleting:protected] => 
            )

        [1] => App\QuizGroupQuestionId Object
            (
                [fillable:protected] => Array
                    (
                        [0] => quiz_id
                        [1] => group_id
                        [2] => question_id
                    )

                [dates:protected] => Array
                    (
                        [0] => deleted_at
                    )

                [connection:protected] => mysql
                [table:protected] => 
                [primaryKey:protected] => id
                [keyType:protected] => int
                [incrementing] => 1
                [with:protected] => Array
                    (
                    )

                [withCount:protected] => Array
                    (
                    )

                [perPage:protected] => 15
                [exists] => 1
                [wasRecentlyCreated] => 
                [attributes:protected] => Array
                    (
                        [id] => 13
                        [quiz_id] => 1
                        [group_id] => 9
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 13
                        [quiz_id] => 1
                        [group_id] => 9
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [casts:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [appends:protected] => Array
                    (
                    )

                [events:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [relations:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [timestamps] => 1
                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [forceDeleting:protected] => 
            )

        [2] => App\QuizGroupQuestionId Object
            (
                [fillable:protected] => Array
                    (
                        [0] => quiz_id
                        [1] => group_id
                        [2] => question_id
                    )

                [dates:protected] => Array
                    (
                        [0] => deleted_at
                    )

                [connection:protected] => mysql
                [table:protected] => 
                [primaryKey:protected] => id
                [keyType:protected] => int
                [incrementing] => 1
                [with:protected] => Array
                    (
                    )

                [withCount:protected] => Array
                    (
                    )

                [perPage:protected] => 15
                [exists] => 1
                [wasRecentlyCreated] => 
                [attributes:protected] => Array
                    (
                        [id] => 14
                        [quiz_id] => 1
                        [group_id] => 10
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [original:protected] => Array
                    (
                        [id] => 14
                        [quiz_id] => 1
                        [group_id] => 10
                        [question_id] => 
                        [created_at] => 2017-07-22 18:56:54
                        [updated_at] => 2017-07-22 18:56:54
                        [deleted_at] => 
                    )

                [casts:protected] => Array
                    (
                    )

                [dateFormat:protected] => 
                [appends:protected] => Array
                    (
                    )

                [events:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [relations:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [timestamps] => 1
                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [forceDeleting:protected] => 
            )

    )

)

rendered html code:

    <table class="table table-striped table-bordered table-hover table-responsive">
<thead>
<tr>
<th><label><input name="group_all" class="checked_all" type="checkbox"> Select All</label></th>
<th>#</th>
<th>Group Name</th>
</tr>
</thead>
<tbody> 
    <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="4" class="check_checkbox"  checked  ></label></td>
        <td>1</td>
        <td>jkl</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="5" class="check_checkbox"  checked  ></label></td>
        <td>2</td>
        <td>mno</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="7" class="check_checkbox"  checked  ></label></td>
        <td>3</td>
        <td>rthytr4y</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="8" class="check_checkbox"  ></label></td>
        <td>4</td>
        <td>erter</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="9" class="check_checkbox"  ></label></td>
        <td>5</td>
        <td>wetret</td>
    </tr>
                                                                                <tr>
        <td><label><input type="checkbox" name="groups_id[]" value="10" class="check_checkbox"  ></label></td>
        <td>6</td>
        <td>yhjjyukji</td>
    </tr>
    </tbody>
</table>
MD Iyasin Arafat
  • 763
  • 1
  • 12
  • 22
  • i'm not familiar with blade/laravel, but i can tell you how to debug. you could do print_r($qids). before these code block, and see what's inside $qids. you can see the resulting html, which radiobutton has the "checked" printed. – r4ccoon Jul 22 '17 at 19:25
  • there is array of qids. showing array list. Do you tell me how i will checked checkbox value in this way above my code. – MD Iyasin Arafat Jul 22 '17 at 19:28
  • give us your rendered html – r4ccoon Jul 22 '17 at 19:31
  • Hello @r4ccoon I'm update my question ans with print_r code and rendered html code and i'm using html checkbox not radio buttons. that's why checked multiple input. – MD Iyasin Arafat Jul 22 '17 at 19:39
0

ok, you can see that all radio buttons are checked. you need to have better check. use this check, please fix the syntax according to the blade syntax. but basically you are comparing group_id in $qids with id in $data.

@if(isset($qids[$key]->group_id) && $qids[$key]->group_id == $data->$id ) {{ 'checked' }} @endif
r4ccoon
  • 3,056
  • 4
  • 26
  • 32
  • Same problem, it's checked first checkbox after checking last checkbox. – MD Iyasin Arafat Jul 22 '17 at 19:50
  • 1
    you need to track your last checked id/ submitted it. easiest i can think of is to put it in the url parameter. the idea is, once submitted, redirect to e.g.: page.php?last_checked_id=3 than you compare it in your radiobutton line. if($data->id == $_GET['last_checked_id') if it worked, you need to make your code pretty by using laravel way to get GET parameter. and then think a bit about the security issues (if any) caused by this. – r4ccoon Jul 22 '17 at 19:55
  • Thanks so much for your valuable ans. – MD Iyasin Arafat Jul 22 '17 at 20:01
  • Hi @r4ccoon I'm solved my this problem and added those code. Thanks again for your help :) – MD Iyasin Arafat Jul 25 '17 at 14:02