laravel 中的 html 表格设计中的 foreach 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44140230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:01:47  来源:igfitidea点击:

foreach loop inside html table design in laravel

laravelforeachlaravel-5.4

提问by 06011991

I have an array of data to display using table in html .but the foreach loop which i m using is not giving the desired format . Below is the array data

我有一组数据要使用 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
        )
)

my view html code :

我的视图 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

By using above for each loop i m getting the below html format like bundle nameis repeating .enter image description here

通过对每个循环使用上面的内容,我会得到下面的 html 格式,就像bundle name重复一样。在此处输入图片说明

But i need the output should be like as below : enter image description here

但我需要输出应该如下所示: 在此处输入图片说明

that means i want the bundle name shluld come only one time and the rest of details should display as in rows . How do i do that ? any suggestions please ? Thank you .

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

回答by manian

Please try the below code,
I have assumed the below points,
1. Your bundle_name will change(that is you will have various bundle_name). This code will work even if you have single bundle_name.
2. You will sort the result by bundle_name.
3. You need bundle title & table header for each bundle_name(again, this code will work even if you have single bundle_name).
4. bundle_name will never have a value false.

请尝试下面的代码,
我已经假设了以下几点,
1. 你的bundle_name 会改变(也就是说你会有不同的bundle_name)。即使您只有一个 bundle_name,此代码也能正常工作。
2. 您将按 bundle_name 对结果进行排序。
3. 您需要为每个 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>