【问题标题】:Display array values in table format in Laravel 5.1在 Laravel 5.1 中以表格格式显示数组值
【发布时间】: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,即affiliateprincipal_moderator。我想用该数据而不是 2 个不同的行填充单行。

我如何实现它??

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

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

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

【问题讨论】:

  • 首先,出于性能原因,我建议您对此查询进行分页
  • 我已经使用datatables.net的插件完成了这项工作
  • 我认为问题在于您在 if-else 语句中的$product['accountType']。请重新访问。
  • 这是什么,那你能指导我正确的方法吗?

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


【解决方案1】:

您应该对行进行分组并将 accountType 属性设置为数组。基本上创建新数组,遍历产品并将它们添加到新数组中(如果尚未添加),如果它们已添加,则将 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 属性中。

【讨论】:

  • 您的解决方案有效,但存在一个问题。在数组中,某些产品具有相同的product_id 但具有不同的option_code(默认为空白),我想要它们也将被显示出来。我该怎么做?
  • 在修改已经存在的分组产品时,检查 option_code 是否为空,如果不是,则将其设置为分组产品。 if($product['option_code'] != '') { $grouped[$product['product_id']]['option_code'] = $product['option_code']; } 只要每个产品只有一个 option_code 就应该可以正常工作。
  • 1 个产品可以有多个 option_code。例如,可以有一块布 - 产品 1 - 具有不同的尺寸 [opt-1 => 'OPT-1-1', opt -1 => 'OPT-1-2', etc etc] 这里有多个 option_code (OPT-1-1, OPT-1-2).. 我该怎么做?
  • 与 accountType 方法相同-将其转换为数组并不断添加新的。
  • 好的.. 那我该如何检查呢??
猜你喜欢
  • 2014-04-26
  • 2017-01-01
  • 1970-01-01
  • 2020-03-06
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 2015-07-24
相关资源
最近更新 更多