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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







5















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









share|improve this question

























  • How you detect the Dept: Dept 1 section? did you need 2 tables that one is for dept 1 and the second for dept 2 or both of them should be in one table?

    – Nasser
    Jan 3 at 4:54




















5















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









share|improve this question

























  • How you detect the Dept: Dept 1 section? did you need 2 tables that one is for dept 1 and the second for dept 2 or both of them should be in one table?

    – Nasser
    Jan 3 at 4:54
















5












5








5








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









share|improve this question
















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






php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 8:59









Umar Abdullah

717419




717419










asked Jan 3 at 4:44









shhrzlshhrzl

1371111




1371111













  • How you detect the Dept: Dept 1 section? did you need 2 tables that one is for dept 1 and the second for dept 2 or both of them should be in one table?

    – Nasser
    Jan 3 at 4:54





















  • How you detect the Dept: Dept 1 section? did you need 2 tables that one is for dept 1 and the second for dept 2 or both of them should be in one table?

    – Nasser
    Jan 3 at 4:54



















How you detect the Dept: Dept 1 section? did you need 2 tables that one is for dept 1 and the second for dept 2 or both of them should be in one table?

– Nasser
Jan 3 at 4:54







How you detect the Dept: Dept 1 section? did you need 2 tables that one is for dept 1 and the second for dept 2 or both of them should be in one table?

– Nasser
Jan 3 at 4:54














1 Answer
1






active

oldest

votes


















2














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





share|improve this answer


























  • can you explain the code about group the data? using this code it can display like i need. but with error. if i remove @foreach ($claims as $idx => $claim) section. it's ok. the error is Trying to get property of non-object. It's referring to this $claim->FullName

    – shhrzl
    Jan 3 at 5:22











  • Is it possible you have a claim in your original array without a valid FullName? The loops assume all the array elements to be uniform and complete.

    – DigitalDrifter
    Jan 3 at 5:34











  • data_get and data_set are helper methods in Laravel for getting and setting data in arrays / objects. data_get will return null if the provided index is not set / null.

    – DigitalDrifter
    Jan 3 at 5:37











  • it error syntax error, unexpected '?'

    – shhrzl
    Jan 3 at 5:42











  • is it correct syntax data_set($groups, $item->EntityName, [ ...$group, $item ]);? it has ...$group

    – shhrzl
    Jan 3 at 5:42












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016465%2fhow-can-i-display-a-group-of-data-in-table-format%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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





share|improve this answer


























  • can you explain the code about group the data? using this code it can display like i need. but with error. if i remove @foreach ($claims as $idx => $claim) section. it's ok. the error is Trying to get property of non-object. It's referring to this $claim->FullName

    – shhrzl
    Jan 3 at 5:22











  • Is it possible you have a claim in your original array without a valid FullName? The loops assume all the array elements to be uniform and complete.

    – DigitalDrifter
    Jan 3 at 5:34











  • data_get and data_set are helper methods in Laravel for getting and setting data in arrays / objects. data_get will return null if the provided index is not set / null.

    – DigitalDrifter
    Jan 3 at 5:37











  • it error syntax error, unexpected '?'

    – shhrzl
    Jan 3 at 5:42











  • is it correct syntax data_set($groups, $item->EntityName, [ ...$group, $item ]);? it has ...$group

    – shhrzl
    Jan 3 at 5:42
















2














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





share|improve this answer


























  • can you explain the code about group the data? using this code it can display like i need. but with error. if i remove @foreach ($claims as $idx => $claim) section. it's ok. the error is Trying to get property of non-object. It's referring to this $claim->FullName

    – shhrzl
    Jan 3 at 5:22











  • Is it possible you have a claim in your original array without a valid FullName? The loops assume all the array elements to be uniform and complete.

    – DigitalDrifter
    Jan 3 at 5:34











  • data_get and data_set are helper methods in Laravel for getting and setting data in arrays / objects. data_get will return null if the provided index is not set / null.

    – DigitalDrifter
    Jan 3 at 5:37











  • it error syntax error, unexpected '?'

    – shhrzl
    Jan 3 at 5:42











  • is it correct syntax data_set($groups, $item->EntityName, [ ...$group, $item ]);? it has ...$group

    – shhrzl
    Jan 3 at 5:42














2












2








2







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 5:57

























answered Jan 3 at 5:01









DigitalDrifterDigitalDrifter

8,8382825




8,8382825













  • can you explain the code about group the data? using this code it can display like i need. but with error. if i remove @foreach ($claims as $idx => $claim) section. it's ok. the error is Trying to get property of non-object. It's referring to this $claim->FullName

    – shhrzl
    Jan 3 at 5:22











  • Is it possible you have a claim in your original array without a valid FullName? The loops assume all the array elements to be uniform and complete.

    – DigitalDrifter
    Jan 3 at 5:34











  • data_get and data_set are helper methods in Laravel for getting and setting data in arrays / objects. data_get will return null if the provided index is not set / null.

    – DigitalDrifter
    Jan 3 at 5:37











  • it error syntax error, unexpected '?'

    – shhrzl
    Jan 3 at 5:42











  • is it correct syntax data_set($groups, $item->EntityName, [ ...$group, $item ]);? it has ...$group

    – shhrzl
    Jan 3 at 5:42



















  • can you explain the code about group the data? using this code it can display like i need. but with error. if i remove @foreach ($claims as $idx => $claim) section. it's ok. the error is Trying to get property of non-object. It's referring to this $claim->FullName

    – shhrzl
    Jan 3 at 5:22











  • Is it possible you have a claim in your original array without a valid FullName? The loops assume all the array elements to be uniform and complete.

    – DigitalDrifter
    Jan 3 at 5:34











  • data_get and data_set are helper methods in Laravel for getting and setting data in arrays / objects. data_get will return null if the provided index is not set / null.

    – DigitalDrifter
    Jan 3 at 5:37











  • it error syntax error, unexpected '?'

    – shhrzl
    Jan 3 at 5:42











  • is it correct syntax data_set($groups, $item->EntityName, [ ...$group, $item ]);? it has ...$group

    – shhrzl
    Jan 3 at 5:42

















can you explain the code about group the data? using this code it can display like i need. but with error. if i remove @foreach ($claims as $idx => $claim) section. it's ok. the error is Trying to get property of non-object. It's referring to this $claim->FullName

– shhrzl
Jan 3 at 5:22





can you explain the code about group the data? using this code it can display like i need. but with error. if i remove @foreach ($claims as $idx => $claim) section. it's ok. the error is Trying to get property of non-object. It's referring to this $claim->FullName

– shhrzl
Jan 3 at 5:22













Is it possible you have a claim in your original array without a valid FullName? The loops assume all the array elements to be uniform and complete.

– DigitalDrifter
Jan 3 at 5:34





Is it possible you have a claim in your original array without a valid FullName? The loops assume all the array elements to be uniform and complete.

– DigitalDrifter
Jan 3 at 5:34













data_get and data_set are helper methods in Laravel for getting and setting data in arrays / objects. data_get will return null if the provided index is not set / null.

– DigitalDrifter
Jan 3 at 5:37





data_get and data_set are helper methods in Laravel for getting and setting data in arrays / objects. data_get will return null if the provided index is not set / null.

– DigitalDrifter
Jan 3 at 5:37













it error syntax error, unexpected '?'

– shhrzl
Jan 3 at 5:42





it error syntax error, unexpected '?'

– shhrzl
Jan 3 at 5:42













is it correct syntax data_set($groups, $item->EntityName, [ ...$group, $item ]);? it has ...$group

– shhrzl
Jan 3 at 5:42





is it correct syntax data_set($groups, $item->EntityName, [ ...$group, $item ]);? it has ...$group

– shhrzl
Jan 3 at 5:42




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016465%2fhow-can-i-display-a-group-of-data-in-table-format%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory