在Laravel 5.1中以表格格式显示数组值

时间:2016-04-07 07:42:45

标签: php arrays laravel laravel-5.1 html-table

我有一个像这样的数组:

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 ...

]

到目前为止,我尝试过的代码在某种程度上正常运行。

<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

上面的代码有效,但它在表中添加了一个新行,这是我不想要的。我不想添加新行,而只想在该行中显示所需数据。

含义,对于product_id = 24,(除了选项代码),附加了2 accountTypeaffiliate和{{1} }。我想用这些数据填充一行,而不是两行。

我如何实现它?

我知道这一定很容易,但我没有解决,因为我还处于学习阶段。

非常感谢任何帮助。感谢。

P.S。:所有值都来自数据库,子数组的数量可以是无限的。

1 个答案:

答案 0 :(得分:0)

您应该对行进行分组并使accountType属性成为一个数组。基本上创建新数组,循环遍历产品并将它们添加到新数组中(如果尚未添加它们),然后将它们添加到其中。像这样:

$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'];
    }
}

然后使用分组数据显示数据,并在accountType上检查您是否在accountTypes属性中检查该类型。