【问题标题】:How to print two array in one foreach loop in Laravel如何在 Laravel 的一个 foreach 循环中打印两个数组
【发布时间】:2017-12-28 17:06:09
【问题描述】:

我在这个 forech 循环中打印这两个数组。如果我在提交后选择最后一个复选框项目,它将检查第一个项目没有检查最后一个项目。那是我的问题。

这是我的代码示例图片

问题

我也在尝试其他问题的答案,但对我不起作用。

  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?

提前致谢。

【问题讨论】:

  • 发布代码。不是图片
  • 好的@Andreas。我会补充的。
  • 尝试为您的复选框输入使用不同的 id 和类
  • 通过在 html 中回显您的 $qids[$key]->group_id 来调试它。您仅使用isset进行检查。您是否还想根据 $data->group_id == $qids[$key]->group_id 进行检查?除非您也发布代码,否则我无法准确判断
  • 嗨 @RAUSHANKUMAR 尝试使用不同的 id 和类。但没有任何改变。

标签: php arrays foreach laravel-blade


【解决方案1】:

我的刀片代码:

@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

这是 print_r 代码:

Illuminate\Database\Eloquent\Collection Object

( [项目:受保护] => 数组 ( [0] => App\QuizGroupQuestionId 对象 ( [可填充:受保护] => 数组 ( [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] => 
            )

    )

)

渲染的html代码:

    <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>

【讨论】:

  • 我不熟悉blade/laravel,但我可以告诉你如何调试。你可以做 print_r($qids)。在这些代码块之前,看看 $qids 里面有什么。您可以看到生成的 html,其中单选按钮打印了“已选中”。
  • 有 qids 数组。显示数组列表。你告诉我我将如何在我的代码上方以这种方式检查复选框值。
  • 给我们你渲染的html
  • 你好@r4ccoon 我正在用 print_r 代码和呈现的 html 代码更新我的问题和我使用的是 html 复选框而不是单选按钮。这就是为什么要检查多个输入。
【解决方案2】:

好的,您可以看到所有单选按钮都已选中。你需要有更好的检查。使用此检查,请根据刀片语法修复语法。但基本上你是在比较 $qids 中的 group_id 和 $data 中的 id。

@if(isset($qids[$key]->group_id) && $qids[$key]->group_id == $data->$id ) {{ 'checked' }} @endif

【讨论】:

  • 同样的问题,在检查最后一个复选框后检查第一个复选框。
  • 您需要跟踪您上次检查的 id/提交它。我能想到的最简单的方法是将它放在 url 参数中。这个想法是,一旦提交,重定向到例如:page.php?last_checked_id=3,而不是在单选按钮行中进行比较。 if($data->id == $_GET['last_checked_id') 如果有效,您需要使用 laravel 获取 GET 参数的方式使您的代码更漂亮。然后考虑一下由此引起的安全问题(如果有的话)。
  • 非常感谢您的宝贵意见。
  • 嗨@r4ccoon 我已经解决了我的这个问题并添加了这些代码。再次感谢您的帮助:)
【解决方案3】:

我使用两个 php 数组函数解决了我的问题。

  1. array_column
  2. in_array

这是我解决的代码:

    @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

截图:

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2019-08-09
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多