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

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

Related

how do i access the array in view after passing it from controller [duplicate]

This question already has answers here:
How to loop multidimensional array in laravel blade
(3 answers)
Closed 4 months ago.
return view($this->folder_path . 'index', compact('data' ));
I want to use foreach to display data.
array:5 [▼
0 => array:4 [▶]
1 => array:4 [▼
"id" => 2
"name" => "test"
"desc" => "my name"
"age" => 12
]
This is view where i want to display data
#foreach($data as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ ucwords($item->name) }}</td>
<td>{{ $item->desc }}</td>
<td>{{ $item->age }}</td>
<tr>
#endforeach
Your $data and each $item appear to be arrays and not objects, therefore using the object accessor (->) on $item will not work. Instead you should be accessing the elements of your $item array using their key names:
#foreach($data as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ ucwords($item['name']) }}</td>
<td>{{ $item['desc'] }}</td>
<td>{{ $item['age'] }}</td>
<tr>
#endforeach
Probably worthwhile doing some reading on associative arrays.

Print Array on Laravel html table

I have some data that returns to me as a laravel array and I want to print it out in a table in a view, but I can't access that data
array:1 [▼
0 => array:2 [▼
0 => array:3 [▼
0 => "MS3939"
1 => "20G1"
2 => "20DC"
]
1 => array:3 [▼
0 => "MRK940"
1 => "20R1"
2 => "20RF"
]
]
]
this is my view code with the table
<table class="table table-bordered">
<thead>
<tr class="text-center">
<th>ID</th>
<th>ISO</th>
<th>Type</th>
</tr>
</thead>
<tbody id="filter-table">
#foreach($datos as $dato)
<tr>
<th scope="row">{{$loop->iteration}}</th>
<td>{{$dato['0']}}</td>
<td>{{$dato['1']}}</td>
<td>{{$dato['2']}}</td>
#endforeach
</tr>
</tbody>
</table>
If your $datos is a collection then do one thing.
$datos = $datos->flatten(1); //it will reduce one level of your array
If your $datos is not a collection and it just an array then convert it to collection
$datos = collect($datos); //then
$datos = $datos->flatten(1);
Then in view you are already running a loop, so you will be able to access data like...
<td>{{ $dato[0] }}</td>
<td>{{ $dato[1] }}</td>
<td>{{ $dato[2] }}</td>

Trying to get property of non-object from blade view in laravel

I try to display my pagination data to database ...My pagination data was from json that i already decode .....i was able to pass to my pagination data to view but not able to display in table
....
This is my error from blade view
Trying to get property of non-object
my result pagination
LengthAwarePaginator {#4177 ▼
#total: 1
#lastPage: 1
#items: Collection {#4169 ▼
#items: array:1 [▼
"data" => array:852 [▼
0 => {#624 ▶}
1 => {#623 ▶}
2 => {#628 ▼
+"ID": "cust1"
+"PWD": "W6ph5Mm5Pz8GgiULbPgzG37mj9g="
+"NICK_NAME": "CUST1"
+"PWD_INVALID_HIT": "0"
+"PWD_CREATE_DT": "2019/07/22 15:57:15"
+"INSTITUTION_ID": "C01"
+"ACL_ID": "L04"
+"LOGIN_DT": "2019/07/22 15:57:15"
+"LOGIN_STS_ID": "N"
+"STS_ID": "C08"
+"TYPE_ID": "U00"
+"UPD_ID": "sufyzasukor"
+"UPD_DT": "2019/07/22 15:57:15"
+"EMAIL_ID": "cust1#gmail.com"
+"PHONE_NO_ID": "0"
+"HP_ID": "0 "
+"CRT_DATE_DELETED": null
+"irel__com_access_level": {#621 …9}
+"irel__com_user_types": {#630 …5}
+"irel__com_status": {#631 …5}
}
}
#perPage: "15"
#currentPage: 1
#path: "http://localhost/IFICSV3/public/configuration/comuserprofiles"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [▶]
}
My controller
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents());
$total = count($content) ;
$collection = new \Illuminate\Support\Collection($content);
$paginationRecord = CollectionPaginate::paginate($collection, $total, '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord]);
}
my blade table that i want to display
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
<tr>
<td>{{ $user->ID }}</td>
<td>{{ $user->NICK_NAME }}</td>
<td>{{ $user->PWD_INVALID_HIT }}</td>
</tr>
#endforeach
dd($user)
array:852 [▼
0 => array:20 [▶]
1 => array:20 [▶]
2 => array:20 [▼
"ID" => "cust1"
"PWD" => "W6ph5Mm5Pz8GgiULbPgzG37mj9g="
"NICK_NAME" => "CUST1"
"PWD_INVALID_HIT" => "0"
"PWD_CREATE_DT" => "2019/07/22 15:57:15"
"INSTITUTION_ID" => "C01"
"ACL_ID" => "L04"
"LOGIN_DT" => "2019/07/22 15:57:15"
"LOGIN_STS_ID" => "N"
"STS_ID" => "C08"
"TYPE_ID" => "U00"
"UPD_ID" => "sufyzasukor"
"UPD_DT" => "2019/07/22 15:57:15"
"EMAIL_ID" => "cust1#gmail.com"
"PHONE_NO_ID" => "0"
"HP_ID" => "0 "
"CRT_DATE_DELETED" => null
"irel__com_access_level" => array:9 [ …9]
"irel__com_user_types" => array:5 [ …5]
"irel__com_status" => array:5 [ …5]
How do i display data in table
You are trying to access a array as a object . That's why the error . Try this .
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
<tr>
<td>{{ $user['ID'] }}</td>
<td>{{ $user['NICK_NAME'] }}</td>
<td>{{ $user['PWD_INVALID_HIT'] }}</td>
</tr>
#endforeach
I think it's cause you have a collection in your $user variable. You need to loop over that $user var like this:
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
#foreach($user as $i => $u)
<tr>
<td>{{ $u['ID'] }}</td>
<td>{{ $u['NICK_NAME'] }}</td>
<td>{{ $u['PWD_INVALID_HIT'] }}</td>
</tr>
#endforeach
#endforeach
Also, do you need this:
#php
$currentRecordno = 1;
#endphp
The $i in your foreach loop should represent the index or $currentRecordno. But if you have good reason, then stick with it.
For readability:
But really, I probably wouldn't do that extra foreach loop.
I think your controller method should be more like:
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents());
$paginationRecord = CollectionPaginate::paginate($content['data'], count($content), '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord]);
}
Actually your blade will need to change to this:
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
<tr>
<td>{{ $user['ID'] }}</td>
<td>{{ $user['NICK_NAME'] }}</td>
<td>{{ $user['PWD_INVALID_HIT'] }}</td>
</tr>
#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

Display array values in table format in Laravel 5.1

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.

Categories