Reputation: 461
I have
table called "matrixs", field are (kriteria1, kriteria2, grade)
in my mvc I did this :
First by calling my model called "matrix" in "AHPController" :
$alltable = Matrix::all(); return view('result', compact('alltable'));
and called it in "result" view :
@foreach($alltable as $r1)
<tr>
<td>{{ $r1->kriteria1 }}</td>
<td>{{ $r1->kriteria2 }}</td>
<td>{{ $r1->grade }}</td>
</tr>
@endforeach
I got this result
how to echo it in blade so it become like this :
just ignore the color, only need the matrix shown in blade in that format. Thank you.
Upvotes: 0
Views: 4102
Reputation: 1892
What I would do in this case is first build up a 2 dimensional array and then loop over that array in your view.
// In your controller method
$records = Matrix::orderBy('kriteria2')
->orderBy('kriteria1')
->get();
$rows = [];
$columns = [];
foreach($records as $index => $record) {
// Create an empty array if the key does not exist yet
if(!isset($rows[$record->kriteria1])) {
$rows[$record->kriteria1] = [];
}
// Add the column to the array of columns if it's not yet in there
if(!in_array($record->kriteria2, $columns)) {
$columns[] = $record->kriteria2;
}
// Add the grade to the 2 dimensional array
$rows[$record->kriteria1][$record->kriteria2] = $record->grade;
}
Then in your view you can loop over it like this:
<table>
<thead>
<tr>
<th><!-- Empty for the left top corner of the table --></th>
@foreach($columns as $column)
<th>{{ $column }}</th>
@endforeach
</tr>
</thead>
<tbody>
@foreach($rows as $kriteria1 => $columns)
<tr>
<td><strong>{{ $kriteria1 }}</strong></td>
@foreach($columns as $kriteria2 => $grade)
<td>{{ $grade }}</td>
@endforeach
</tr>
@endforeach
</tbody>
</table>
Update for second question in comment below:
// In your controller method
$records = Matrix::orderBy('kriteria2')
->orderBy('kriteria1')
->get();
// Build up a map like this from the data with the names
$names = [
'kriteria1' => 'name1',
'kriteria2' => 'name2',
'kriteria3' => 'name3',
];
$rows = [];
$columns = [];
foreach($records as $index => $record) {
$name1 = $names[$record->kriteria1];
$name2 = $names[$record->kriteria2];
// Create an empty array if the key does not exist yet
if(!isset($rows[name1])) {
$rows[name1] = [];
}
// Add the column to the array of columns if it's not yet in there
if(!in_array(name2, $columns)) {
$columns[] = name2;
}
// Add the grade to the 2 dimensional array
$rows[name1][name2] = $record->grade;
}
Upvotes: 1
Reputation: 33068
This should get you close, you just need to be clever on how you use colspan
and rowspan
attributes on the td
elements.
<tr>
<td colspan="2"></td>
<td colspan="{{ count(array_keys($alltable->first()->toArray())) }}">kriteria2</td>
</tr>
<tr>
<td colspan="2"></td>
@foreach(array_keys($alltable->first()->toArray()) as $i => $val)
<td>{{ $i }}</td>
@endforeach
</tr>
@foreach($alltable as $i => $r1)
<tr>
@if($i == 0)
<td rowspan="{{ $alltable->count() }}"></td>
@endif
<td>{{ $i }}</td>
<td>{{ $r1->kriteria1 }}</td>
<td>{{ $r1->kriteria2 }}</td>
<td>{{ $r1->grade }}</td>
</tr>
@endforeach
Upvotes: 0