【问题标题】:foreach loop inside html table design in laravellaravel中html表格设计中的foreach循环
【发布时间】:2017-05-23 16:22:36
【问题描述】:

我有一个数据数组要使用 html 中的表格显示。但是我使用的 foreach 循环没有给出所需的格式。 下面是数组数据

$data =  Array
(
    [0] => Array
        (
            [id] => 10
            [asset_name] => Mini Paver
            [qty] => 3
            [est_start_date] => 02/05/2017
            [days] => 2
            [comments] => Comment 2
            [bundle_name] => 1XRoller 1XPaver
        )

    [1] => Array
        (
            [id] => 11
            [asset_name] => Roller
            [qty] => 2
            [est_start_date] => 03/07/2018
            [days] => 4
            [comments] => Comment 2
            [bundle_name] => 1XRoller 1XPaver
        )
)

我的视图 html 代码:

@foreach($data as $value) 

<table class="" style="width: 100%;border:1px solid #ccc">
<thead>
 <tr>
    <th colspan="4"> <p><?php echo $value['bundle_name'];?> </p></th>
  </tr>
<tr>
    <th style="text-align: center">id</th>
    <th style="width:5%;text-align: center">Asset Category</th>
    <th style="width:5%;text-align: center">Days</th>
    <th style="width:5%;text-align: center">Qty</th>
</tr>
</thead>
<tbody>

    <tr>
        <th style="text-align: center"><?php echo $value['id'];?> </th>
        <th style="width:5%;text-align: center"><?php echo $value['asset_name'];?></th>
        <th style="width:5%;text-align: center"><?php echo $value['days'];?></th>
        <th style="width:5%;text-align: center"><?php echo $value['qty'];?></th>
    </tr>
                                 
</tbody>
</table>
@endforeach

通过使用上面的每个循环,我得到了下面的 html 格式,如 bundle name 正在重复。

但我需要输出应该如下所示:

这意味着我希望捆绑包名称仅出现一次,其余详细信息应按行显示。 我怎么做 ?请问有什么建议吗? 谢谢。

【问题讨论】:

  • 您的 bundle_name 对所有项目都一样吗?还是会有所不同?

标签: laravel foreach laravel-5.4


【解决方案1】:

请尝试以下代码,
我假设了以下几点,
1.你的bundle_name会改变(也就是说你会有不同的bundle_name)。即使您只有一个 bundle_name,此代码也可以使用。
2. 您将按 bundle_name 对结果进行排序。
3. 您需要每个 bundle_name 的 bundle 标题和表头(同样,即使您有单个 bundle_name,此代码也可以工作)。
4. bundle_name 永远不会有值 false。

@php ($bundle_name = false)
@foreach($data as $value) 
    @if($bundle_name != $value['bundle_name'])
        @if($bundle_name != false)
                </tbody>
            </table>
        @endif
    @php ($bundle_name = $value['bundle_name'])
    <table class="" style="width: 100%;border:1px solid #ccc">
        <thead>
            <tr>
                <th colspan="4"> <p> {{ $bundle_name }} </p></th>
            </tr>
            <tr>
                <th style="text-align: center">id</th>
                <th style="width:5%;text-align: center">Asset Category</th>
                <th style="width:5%;text-align: center">Days</th>
                <th style="width:5%;text-align: center">Qty</th>
            </tr>
        </thead>
        <tbody>
    @endif
            <tr>
                <th style="text-align: center">{{ $value['id'] }} </th>
                <th style="width:5%;text-align: center">{{ $value['asset_name'] }}</th>
                <th style="width:5%;text-align: center">{{ $value['days'] }}</th>
                <th style="width:5%;text-align: center">{{ $value['qty'] }}</th>
            </tr>
@endforeach
</tbody>
</table> 

【讨论】:

    猜你喜欢
    • 2019-02-27
    • 2019-11-17
    • 2015-06-28
    • 1970-01-01
    • 2021-09-07
    • 2023-04-08
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    相关资源
    最近更新 更多