In the code below, $rows is the result of database query. In the view that consumes this, I need to break the results into a separate table for each vendor.
The below creates a structure that allows this in the template, but it seems like a clunky way of doing it. Advice on how to improve it?
$byVendor = [];
if (sizeof($rows) < 1)
return $byVendor;
$vendor = $rows[0]->AMVEND;
$i = 0;
foreach ($rows as $row ) {
$currentVendor = $row->AMVEND;
if ($currentVendor != $vendor)
$i++;
$byVendor[$i][] = $row;
}
return $byVendor;
Code used in the view:
@section('content')
@foreach($vendors as $vendor)
<table class="table">
<caption>{{$vendor[0]->AMNAME}}</caption>
<thead>
<tr>
<th>Date/Time</th>
<th>PO Number</th>
<th>Item</th>
<th>Qty</th>
</tr>
@foreach($vendor as $item)
<tr>
<td>{{$item->CREATED_AT}}</td>
<td>{{$item->SOURCE}}</td>
<td>{{$item->ITEM}}</td>
<td>{{$item->QTY}}</td>
</tr>
@endforeach
</thead>
<tbody>
</tbody>
</table>
@endforeach
@endsection