【发布时间】:2016-07-27 22:53:05
【问题描述】:
我有一个这样的数组:
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,(除了选项代码)有两个accountType,即affiliate和principal_moderator。我想用该数据而不是 2 个不同的行填充单行。
我如何实现它??
我知道这一定很容易,但我没能解决它,因为我还处于学习阶段。
非常感谢任何帮助。谢谢。
P.S.:所有值都来自数据库,子数组的数量可以是无限的。
【问题讨论】:
-
首先,出于性能原因,我建议您对此查询进行分页
-
我已经使用datatables.net的插件完成了这项工作
-
我认为问题在于您在 if-else 语句中的
$product['accountType']。请重新访问。 -
这是什么,那你能指导我正确的方法吗?
标签: php arrays laravel laravel-5.1 html-table