Display array values in table format in Laravel 5.1 - php

I have an array like so:
array:55 [▼
0 => array:13 [▼
"product_id" => "1"
"name" => "Glowing Antique Multi-Color Necklace And Hangings"
"product_code" => "DIV-COL-0001-0001"
"option_code" => ""
"net_price" => "693.00"
"sellerCommission" => "450.00"
"moderatorCommission" => "14.00"
"principalModeratorCommission" => "17.00"
"affiliateCommission" => "28.00"
"accountType" => "affiliate"
]
25 => array:13 [▼
"product_id" => "24"
"name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
"product_code" => "PRA-KEN-0002-0006"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "affiliate"
]
26 => array:13 [▼
"product_id" => "25"
"name" => "Side Hanging Purse (2 Nos.)"
"product_code" => "PRA-KEN-0002-0007"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "affiliate"
]
44 => array:13 [▼
"product_id" => "24"
"name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
"product_code" => "PRA-KEN-0002-0006"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "principal_moderator"
]
// And the list continues ...
]
The code that I have tried so far, works correctly to a certain extent.
<tr>
<th style="vertical-align: top;">Id</th>
<th style="vertical-align: top;">Details</th>
<th style="vertical-align: top;">Net Price</th>
<th style="vertical-align: top;">Invoicer<br /> Commission</th>
<th style="vertical-align: top;">Moderator Commission</th>
<th style="vertical-align: top;">Principal Moderator Commission</th>
<th style="vertical-align: top;">Affiliate Commission</th>
</tr>
#foreach($products as $product)
<tr>
<td>{{ $product['product_id'] }}</td>
<td>
<img src="{{ $product['image'] }}" alt="{{ $product['name'] }}" class="pull-left" style="margin-right: 15px">
{{ $product['name'] }}<br />
Product Code: {{ $product['product_code'] }}<br />
#if($product['option_code'] !== '')
Option Code: {{ $product['option_code'] }}
#endif
</td>
<td class="text-right">{{ $product['net_price'] }}</td>
#if($product['accountType'] === 'seller')
<td class="text-right">{{ number_format($product['sellerCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'moderator')
<td class="text-right">{{ number_format($product['moderatorCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'principal_moderator')
<td class="text-right">{{ number_format($product['principalModeratorCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'affiliate')
<td class="text-right">{{ number_format($product['affiliateCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
</tr>
#endforeach
The above code works, but it adds a new row to the table which is what I am not looking for. I do not want to add a new row, instead I would like to display the required data in that row only.
Meaning, for the product_id = 24, (except for the option code), there are 2 accountType attached viz., affiliate and principal_moderator. I would like to populate a single row with that data instead of 2 different rows.
How do I achieve it ??
I know this must be far easy, but I have failed to solve it, because I am still at the learning stage.
Any help is highly appreciated. Thanks.
P.S.: All the values are coming from the database, and the number of child arrays can be infinite.

You should group rows and make accountType property an array. Basically create new array, loop through products and add them into new array if they are not already added and if they are then add accountType to it. Something like this:
$grouped = array();
foreach($products as $product) {
if(!array_key_exists($product['product_id'], $grouped)) {
$grouped[$product['product_id']] = $product;
$grouped[$product['product_id']]['accountTypes'] = array($product['accountType']);
}else {
$grouped[$product['product_id']]['accountTypes'][] = $product['accountType'];
}
}
Then you use grouped data to display data and on accountType check you check if that type exists in accountTypes property.

Related

Convert Array data to html table

im having some issues with a array data that i got form a json file, but i guess the issue maybe is with the json structure that that i converted in array using json decode and file_get_contents.
Basicall this is the array strucutre:
#items: array:4 [▼
"Monday, 1 de Fev de 2021" => array:5 [▼
"PM" => array:7 [▼
1 => "1140-10"
2 => "8498-25"
3 => "7076-19"
4 => "3380-20"
5 => "8194-24"
6 => "8288-22"
7 => "687-22"
]
"PT" => array:7 [▼
1 => "6406-2"
2 => "2976-19"
3 => "6029-8"
4 => "8130-8"
5 => "7530-8"
6 => "1071-18"
7 => "064-16"
]
"PTV" => array:7 [▶]
"PTN" => array:7 [▶]
]
"Sat, 31 de Jan de 2021" => array:2 [▼
"PTM" => array:7 [▶]
"PT" => array:7 [▶]
]
Basically the date is the table caption, and the index with the letters eg: "PTV","PM","PT"...", are the theader th titles.
And inside of the "PM" for example there are some results:
1 => "1140-10"
2 => "8498-25"
3 => "7076-19"
4 => "3380-20"
5 => "8194-24"
6 => "8288-22"
7 => "687-22"
Where the indexes (1,2,3..) Are the prize, and the values after each index are the results.
I need to construct my table to be like this image above
This is my code:
#if(sizeof($results) > 0)
#foreach($results as $date => $result)
<div class="col-xl-12">
<table class="table">
<caption>
{{ $date }}
</caption>
<thead>
<tr>
<th id="hoje" class="tabla-header"></th>
#foreach($result as $banca => $re)
<th id="{{ $banca }}" class="tabla-header">{{ $banca }}</th>
#endforeach
</tr>
</thead>
<tbody>
{{ sizeof($result) }}
#for ($i = 0; $i < 7; $i++)
<tr>
<td>1</td>
#for ($n = 0; $n < sizeof($result); $n++)
<td>{{ $result[] }}/td>
#endfor
</tr>
#endfor
</tbody>
</table>
</div>
#endforeach
#endif
create a database and insert into table information . next with console make:controller // make a controller
make a method ...( select information of database)
$var1 = DB::table("table_name")->get('column_1');// first use DB namespace
$var2 = DB::table("table_name")->get('column_2');
$var2 = $var2[
// and other get columns table
return view('view_name',['tr1' => $var1,'tr2' => $var2 /* and etc */);
}
and view is under code
<!DOCTYPE htl>
<html>
<table class="table"> <!-- table class ; you create a table class in your code -->
<tr>
<th>{{$var1[0][0]->column_name}}</th>
<th>{{$var1[0][1]->column_name}}</th>
<th>{{$...}}</th>
</tr>
<tr>
<th> {{$avr2[0][0]->column_name}} </th>
<th> {{$var2[0][1]->coumn_name}} </th>
<!-- and continue above code -->
</tr>
</table>
</html>
first you select info of db and show them in view

PHP multidimensional arrays with foreach troubles

I have a little problem , I hope you can help me. I want to create a table with some details, about name,surname, age etc...
One array is made by:
array:2 [▼
0 => array:9 [▼
"ID" => 424
"name" => "Alex"
"surname" => "Pippo"
"age" => 23
]
1 => array:9 [▼
"ID" => 424
"name" => "Pippo"
"surname" => "James"
"age" => "21"
]
and I have another multidimensional array as follow:
0 => array:2 [▼
0 => array:3 [▼
"class" => "VA"
"sport" => "football"
"height" => "1.55"
]
1 => array:3 [▼
"class" => "VA"
"sport" => "basketball"
"height" => "1.55"
]
]
1 => array:1 [▼
0 => array:3 [▼
"class" => "VB"
"sport" => "none"
"height" => "1,76"
]
]
]
I want to have have a table in which i have details about students and their sports. So in the view i got:
<table border="1" cellspacing="5">
<th >ID</th>
<th >Name</th>
<th >surName</th>
<th >age</th>
#foreach($data as $datas)
<tr>
<td><?php echo($datas['id']) ?></td>
<td><?php echo($datas['name']) ?></td>
<td><?php echo($datas['surname']) ?></td>
<td><?php echo($datas['age']) ?></td>
<table>
<th>class</th>
<th>sport</th>
<th>age</th>
<tr>
#foreach($data1 as $datas1)
#foreach($datas1 as $key => $value)
<td ><?php echo($value['class']) ?></td>
<td ><?php echo($value['sport']) ?> </td>
<td ><?php echo($value['height']) ?> </td>
</tr>
</table>
#endforeach
#endforeach
#endforeach
</tr>
</table>
I want to have two tables, one inside the other one.
ID Name surName Age
424 Alex Pippo 23
Class Sport Height
VA Football 1,55
VA basketball 1,55
VB none 1,76 *THIS BELONGS TO THE SECOND STUDENT
ID Name surName Age
425 Pippo James 22
Class Sport Height
VA Football 1,55 *THIS BELONGS TO THE FIRST STUDENT
VA basketball 1,55 *THIS BELONGS TO THE FIRST STUDENT
VB none 1,76
i just want for the first one the first 2 rows, and for the second one the third row.. Please help..
Based on your data, it seems that the key of $data1 is the same as the key to your student ($data). So, you need to connect both of them to get their relevant data.
The first step, you need to get the key to your student:
#foreach($data as $keys => $datas)
and only loop this student detail:
#foreach($data1[$keys] as $key => $value)
<tr>
<td ><?php echo($value['class']) ?></td>
<td ><?php echo($value['sport']) ?></td>
<td ><?php echo($value['height']) ?></td>
</tr>
#endforeach

How can I display a group of data in table format?

I have an array to be display in table format.
My table should display like this
Dept: Dept 1
No Name BankCode Amount
1. Name4 656789 119.20
Dept: Dept 2
No Name BankCode Amount
1. Name 1 DREW1CF 2775.24
2. Name 2 DREW1CF 907.28
3. Name 3 EWDR1CF 318.60
And this is my array
array:38 [▼
0 => {#389 ▼
+"FullName": "Name 1"
+"BankCode": "DREW1CF"
+"EntityName": "Dept 2"
+"amount": "2775.24"
}
1 => {#391 ▼
+"FullName": "Name 2"
+"BankCode": "DREW1CF"
+"EntityName": "Dept 2"
+"amount": "907.28"
}
2 => {#392 ▼
+"FullName": "Name 3"
+"BankCode": "EWDR1CF"
+"EntityName": "Dept 2"
+"amount": "318.60"
}
3 => {#393 ▼
+"FullName": "Name 4"
+"BankCode": "656789"
+"EntityName": "Dept 1"
+"amount": "119.20"
}
4 => {#394 ▶}
5 => {#395 ▶}
.....and so on
]
The code in Laravel framework.
Currently i am stuck with my foreach.
If i am doing like below, it will not display like i need.
How can i implement the foreach to be what i need?
I not very good in foreach especially with $key => $value.
#php
$i = 1;
#endphp
#foreach ($getClaim as $claims)
<tr>
<td>#php
echo $i++
#endphp</td>
<td>{{$claims->FullName}}</td>
<td>{{$claims->BankCode}}</td>
<td>{{number_format($claims->amount, 2, '.', ',')}}</td>
</tr>
#endforeach
Group the data before creating the table:
$groupedClaims = array_reduce($claims, function ($groups, $item) {
if ($group = data_get($groups, $item->EntityName)) {
// Using the spread operator
data_set($groups, $item->EntityName, [ $item, ...$group ]);
// Using array merge
data_set($groups, $item->EntityName, array_merge($group, [$item]));
} else {
data_set($groups, $item->EntityName, [ $item ]);
}
return $groups;
}, []);
Then in your view file:
#foreach ($groupedClaims as $dept => $claims)
<tr>
<td colspan="4">Dept: {{ $dept }}</td>
</tr>
<tr>
<td>No</td>
<td>Name</td>
<td>BankCode</td>
<td>Amount</td>
</tr>
#foreach ($claims as $idx => $claim)
<tr>
<td>{{ $idx }}</td>
<td>{{ optional($claim)->FullName ?? 'No FullName' }}</td>
<td>{{ optional($claim)->BankCode ?? 'No BankCode' }}</td>
<td>{{ number_format(optional($claim)->amount ?? 0, 2, '.', ',') }}</td>
</tr>
#endforeach
#endforeach

Laravel Blade Multidimensional Array

I have the below arrays and I want to get it in laravel blade as HTML table
array:2 [▼
"chest" => array:2 [▼
"Chest Press" => array:1 [▼
0 => "1"
]
"Flys" => array:2 [▼
0 => "3"
1 => "4"
]
]
"hints" => array:2 [▼
"Chest Press" => array:1 [▼
0 => "test1"
]
"Flys" => array:1 [▼
0 => "test2"
]
]
]
I try the below but I dont get right HTML table, its correct the first two columns but the third columnt is not, any ideas how to print it on HTML table
<table class="table table-striped table-hover table-reflow">
<thead>
<tr>
<th>Exercises</th>
<th>Days</th>
<th>Hints</th>
</tr>
</thead>
<tbody>
#foreach($chests['chest'] as $chest => $exc)
<tr>
<td>{{$chest}}</td>
#foreach($exc as $key => $value)
<td>
<strong>{{$value}},</strong>
</td>
#endforeach
#endforeach
#foreach($chests['hints'] as $hint => $hin)
#foreach($hin as $key => $value)
<td>
<strong>{{$value}}</strong>
</td>
#endforeach
</tr>
#endforeach
</tbody>
Answered
<tbody>
#foreach ($chests['chest'] as $exerciseName => $daysArray)
<tr>
<td>{{ $exerciseName }}</td>
#foreach ($daysArray as $day)
<td >{{ $day }}</td>
#endforeach
<td>{{ $chests['hints'][$exerciseName][0] }}</td>
</tr>
#endforeach
</tbody>
https://laracasts.com/discuss/channels/laravel/laravel-blade-multidimensional-array

Unable to pass laravel array on to the view

I have an array that i want to return from the controller to the view.But unable to retrieve that.
If i dump my array it looks like this:
array:121 [▼
0 => array:10 [▼
"ProcedureName" => "eye"
"Status" => "referred"
"PreferredDate" => "12/12/2018"
"PreferredCity" => "Bangalore"
"BookingId" => "EZBK000126"
"Documents" => ""
"Name" => "vik kon"
"Age" => 62
"Gender" => "male"
"Mob" => "1110002223"
]
1 => array:10 [▼
"ProcedureName" => "eye"
"Status" => "referred"
"PreferredDate" => "12/12/2018"
"PreferredCity" => "mysore"
"BookingId" => "EZBK000125"
"Documents" => ""
"Name" => "vik kon"
"Age" => 62
"Gender" => "male"
"Mob" => "9146178526"
]
So i want to get it to show inside my view .Currently my code looks like this :
#foreach($new_records as $new_recordss)
<tr>
<td scope="col">{{ $new_recordss->ProcedureName }}</td>
<td scope="col">{{ $new_recordss->Status }}</td>
<td scope="col">{{ $new_recordss->Age }}</td>
<td scope="col"><div class="btn-group">
</div>
</td>
</tr>
#endforeach
But this gives the following error:
Trying to get property of non-object
That's because an array is not an object.
// -> is for object
<td scope="col">{{ $new_recordss->ProcedureName }}</td>
// [] is for array
<td scope="col">{{ $new_recordss['ProcedureName'] }}</td>
Thus this should work:
<td scope="col">{{ $new_recordss['ProcedureName'] }}</td>
<td scope="col">{{ $new_recordss['Status'] }}</td>
<td scope="col">{{ $new_recordss['Age'] }}</td>
The reason why
{{ $new_recordss->ProcedureName }}
would not work is because -> operator is used to access an Object. Notice that you get the following error:
Trying to get property of non-object
Because you have an array with 121 arrays in it. Therefore in order to access this associative array, you would use
{{ $new_recordss['ProcedureName'] }}
Here, the 'ProcedureName' is simply the key name and we are telling the array to fetch it's value. I hope this answers your question.

Categories