1

I have an array of data to display using table in html .but the foreach loop which i m using is not giving the desired format . Below is the array data

$data =  Array
(
    [0] => Array
        (
            [id] => 10
            [asset_name] => Mini Paver
            [qty] => 3
            [est_start_date] => 02/05/2017
            [days] => 2
            [comments] => Comment 2
            [bundle_name] => 1XRoller 1XPaver
        )

    [1] => Array
        (
            [id] => 11
            [asset_name] => Roller
            [qty] => 2
            [est_start_date] => 03/07/2018
            [days] => 4
            [comments] => Comment 2
            [bundle_name] => 1XRoller 1XPaver
        )
)

my view html code :

@foreach($data as $value) 

<table class="" style="width: 100%;border:1px solid #ccc">
<thead>
 <tr>
    <th colspan="4"> <p><?php echo $value['bundle_name'];?> </p></th>
  </tr>
<tr>
    <th style="text-align: center">id</th>
    <th style="width:5%;text-align: center">Asset Category</th>
    <th style="width:5%;text-align: center">Days</th>
    <th style="width:5%;text-align: center">Qty</th>
</tr>
</thead>
<tbody>

    <tr>
        <th style="text-align: center"><?php echo $value['id'];?> </th>
        <th style="width:5%;text-align: center"><?php echo $value['asset_name'];?></th>
        <th style="width:5%;text-align: center"><?php echo $value['days'];?></th>
        <th style="width:5%;text-align: center"><?php echo $value['qty'];?></th>
    </tr>
                                 
</tbody>
</table>
@endforeach

By using above for each loop i m getting the below html format like bundle name is repeating .enter image description here

But i need the output should be like as below : enter image description here

that means i want the bundle name shluld come only one time and the rest of details should display as in rows . How do i do that ? any suggestions please ? Thank you .

1
  • your bundle_name will be same for all the items? or will it vary? Commented May 23, 2017 at 17:16

1 Answer 1

2

Please try the below code,
I have assumed the below points,
1. Your bundle_name will change(that is you will have various bundle_name). This code will work even if you have single bundle_name.
2. You will sort the result by bundle_name.
3. You need bundle title & table header for each bundle_name(again, this code will work even if you have single bundle_name).
4. bundle_name will never have a value false.

@php ($bundle_name = false)
@foreach($data as $value) 
    @if($bundle_name != $value['bundle_name'])
        @if($bundle_name != false)
                </tbody>
            </table>
        @endif
    @php ($bundle_name = $value['bundle_name'])
    <table class="" style="width: 100%;border:1px solid #ccc">
        <thead>
            <tr>
                <th colspan="4"> <p> {{ $bundle_name }} </p></th>
            </tr>
            <tr>
                <th style="text-align: center">id</th>
                <th style="width:5%;text-align: center">Asset Category</th>
                <th style="width:5%;text-align: center">Days</th>
                <th style="width:5%;text-align: center">Qty</th>
            </tr>
        </thead>
        <tbody>
    @endif
            <tr>
                <th style="text-align: center">{{ $value['id'] }} </th>
                <th style="width:5%;text-align: center">{{ $value['asset_name'] }}</th>
                <th style="width:5%;text-align: center">{{ $value['days'] }}</th>
                <th style="width:5%;text-align: center">{{ $value['qty'] }}</th>
            </tr>
@endforeach
</tbody>
</table> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.