jeudi 7 avril 2016

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"
    "format" => "Single"
    "page_url" => "category/5/jewellery/DIV-COL-0001-0001/glowing-antique-multi-color-necklace-and-hangings"
    "image" => "http://localhost:8000/images/products/uploads/dc-0001-0001-75-75.jpg"
    "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"
    "format" => "Single"
    "page_url" => "category/11/bags-and-purses-women/PRA-KEN-0002-0006/blue-colored-cotton-zip-pouch-3-nos"
    "image" => "http://localhost:8000/images/products/uploads/pk-0002-0006-75-75.jpg"
    "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"
    "format" => "Single"
    "page_url" => "category/11/bags-and-purses-women/PRA-KEN-0002-0007/side-hanging-purse-2-nos"
    "image" => "http://localhost:8000/images/products/uploads/pk-0002-0007-75-75.jpg"
    "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"
    "format" => "Single"
    "page_url" => "category/11/bags-and-purses-women/PRA-KEN-0002-0006/blue-colored-cotton-zip-pouch-3-nos"
    "image" => "http://localhost:8000/images/products/uploads/pk-0002-0006-75-75.jpg"
    "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">
            <a href="{{ $product['page_url'] }}" class="links-dark">{{ $product['name'] }}</a><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, 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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire