而& foreach循环到数组的输出

时间:2014-08-12 17:09:00

标签: php laravel while-loop

我有一系列与下面非常相似的地点。我想做的是按具有相同SiteNumbers的项目分组(查看期望的结果),但是我不确定检查当前sitenumber与下一个的最佳方法,看它是否不同,目前我正在检查sitenumber = sitenumber 。欢迎任何建议

array($ locationList):

SiteNumber = 1001, Location = Kitchen, Position = Cupboard
SiteNumber = 1001, Location = Kitchen, Position = Fridge
SiteNumber = 1002, Location = Bedroom, Position = Bed
SiteNumber = 1002, Location = Bedroom, Position = Draws
SiteNumber = 1002, Location = Bedroom, Position = Wardrobe

当前代码:

@while($locationList[0]->SiteNumber = $locationList[0]->CardNumber)

    <h3>{{ $locationList->SiteNumber }}</h3>
            <table class="tbl-striped">
                <tr>
                    <th>Location</th>
                    <th>Position</th>
                </tr>

                @foreach($locationList as $loclist)

                    <tr>
                        <td>{{ $loclist->Location }}</td>
                        <td>{{ $loclist->Position }}</td>

                    </tr>

                @endforeach


            </table>
            @endwhile

期望的结果,看起来像这样:

网站编号= 1001

Location | Position
Kitchen | Cupboard
Kitchen | Fridge

网站编号= 2002

Location | Position
Bedroom | Bed
Bedroom | Draws
Bedroom | Draws

1 个答案:

答案 0 :(得分:0)

您可以使用SQL对sitenumber进行分组。

"SELECT * FROM sites 
 ORDER BY siteNumber"

您可以为每个坐标数创建一个新表格。

$siteNumber = 0;

if($siteNumber !== $locationList->SiteNumber) {
    $siteNumber = $locationList->SiteNumber;
    //Create new table
}

当然,您必须将上述内容转换为Laravel的模板系统语法。