【问题标题】:Is there any function to stop foreach loop? [duplicate]是否有任何功能可以停止 foreach 循环? [复制]
【发布时间】:2019-11-03 09:56:18
【问题描述】:

每当我尝试使用类别名称打印表格第一行打印良好,然后使用 ONE tr(row) 数据打印表格并关闭标签并再次开始新行并打印下一个数据时,我都会设置一个循环一排……

我想打印关于其类别名称的所有数据而不破坏多个表

@if( ! empty($packages) )
    @php
        $serviceId=null;
    @endphp
    @foreach($packages as $package)
        @if(is_null($serviceId))
        <!--CATEGORY NAME PRINT-->
        <div class="row" id="">
          <div class="col-sm-12">
            <div class="well wellHeader">
              <h2>{{ $package->service->name }}</h2>
            </div>
          </div>
        </div>
        <!--CATEGORY NAME PRINT-->
        @php
            $serviceId=$package->service->id
        @endphp

        @endif

        @if($serviceId != $package->service_id)
        <!--CATEGORY NAME PRINT-->
        <div class="row" id="">
          <div class="col-sm-12">
            <div class="well wellHeader">
              <h2>{{ $package->service->name }}</h2>
            </div>
          </div>
        </div>
        <!--CATEGORY NAME PRINT-->
        @php
            $serviceId = $package->service_id
        @endphp
        @endif

    <!--SERVICE NAME TABLE-->
    <div class="row">
        <div class="col-sm-12">
            <div class="well">
                <div class="table-responsive">
                    <table class="table table-striped table-sm ">
                        <thead>
                            <tr>
                              <th>@lang('general.package_id')</th>
                              <th>@lang('general.name')</th>
                              <th>@lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
                              <th>@lang('general.minimum_quantity')</th>
                              <th>@lang('general.maximum_quantity')</th>
                              <th>@lang('general.description')</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                              <td>{{ $package->id }}</td>
                              <td>{{ $package->name }}</td>
                              <td>@php $price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
@endphp
{{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
                              <td>{{ $package->minimum_quantity }}</td>
                              <td>{{ $package->maximum_quantity }}</td>
                              <td>{{ $package->description }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>

    </div>
    !--SERVICE NAME TABLE-->
    @endforeach

    @else
    No Record Found
    @endif

【问题讨论】:

标签: php arrays laravel laravel-4


【解决方案1】:

您可以创建一个额外的数组来映射它,

在方法中

$packagesNew = [];
foreach($packages as $k => $v)
{
  $packagesNew[$v->service->name]['name'] = $v->service->name;
  $packagesNew[$v->service->name]['id'] = $v->service->id;
  $packagesNew[$v->service->name]['service_id'] = $v->service_id;
  $packagesNew[$v->service->name]['data'][] = $v;
}

通过packagesNew查看

然后我在视图中进行了更改,请检查。

@if( ! empty($packagesNew) )
@php
$serviceId=null;
@endphp
@foreach($packagesNew as $k => $package)
@if(is_null($serviceId))
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
    <div class="col-sm-12">
        <div class="well wellHeader">
            <h2>{{ $k }}</h2>
        </div>
    </div>
</div>
<!--CATEGORY NAME PRINT-->
@php
$serviceId=$package['id'];
@endphp
@endif
@if($serviceId != $package['service_id'])
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
    <div class="col-sm-12">
        <div class="well wellHeader">
            <h2>{{ $k }}</h2>
        </div>
    </div>
</div>
<!--CATEGORY NAME PRINT-->
@php
$serviceId = $package['service_id'];
@endphp
@endif
<!--SERVICE NAME TABLE-->
<div class="row">
    <div class="col-sm-12">
        <div class="well">
            <div class="table-responsive">
                <table class="table table-striped table-sm ">
                    <thead>
                        <tr>
                            <th>@lang('general.package_id')</th>
                            <th>@lang('general.name')</th>
                            <th>@lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
                            <th>@lang('general.minimum_quantity')</th>
                            <th>@lang('general.maximum_quantity')</th>
                            <th>@lang('general.description')</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($package['data'] as $k1 => $v1)
                        <tr>
                            <td>{{ $v1->id }}</td>
                            <td>{{ $v1->name }}</td>
                            <td>@php $price = isset($userPackagePrices[$v1->id]) ? $userPackagePrices[$v1->id] : $v1->price_per_item;
                                @endphp
                                {{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
                            <td>{{ $v1->minimum_quantity }}</td>
                            <td>{{ $v1->maximum_quantity }}</td>
                            <td>{{ $v1->description }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
!--SERVICE NAME TABLE-->
@endforeach
@else
No Record Found
@endif

【讨论】:

  • 你的意思是在模型中创建这个方法吗?
  • 您知道,您可能正在模型或控制器中创建包。那里你需要写这段代码
  • 模型和控制器中的代码是用 ioncube 加密的:\有什么办法吗?
  • 试着写在你感觉的地方;)
  • ok 让我试试 :)
猜你喜欢
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 2021-12-25
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多