gpt4 book ai didi

php - Yii2:将具有相同值的Sql数据显示到Kartik gridview中的一列中

转载 作者:行者123 更新时间:2023-11-29 03:22:38 25 4
gpt4 key购买 nike

我正在尝试使用 Yii2 框架开发系统。我的数据库中有数据,我想显示 products 表中的所有数据。例如:

产品表

---------------------------------------------------------------------
**id** | **product** | **color** | **quantity** | **size** |

1 | t-shirt | red | 2 | L |
2 | t-shirt | blue | 3 | M |
3 | pants | black | 6 | M |

我想在 index.php 中将此数据显示到 kartik gridview。但我想按 product 而不是 id 显示数据,所以我的预期结果是这样的 index.php

预期结果

product   |   quantity    |   

t-shirt | 5 |
pants | 6 |

我的预期结果避免,颜色和大小。它将所有相同的产品合并到 gridview 中的一列中。

我怎么会这样?这是我在 index.php 中的代码:

  <?php
use kartik\grid\GridView;

<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn',
'header' => 'No',
],
[
'label' => 'product',
'value' => function($data) {
return $data->product;
}
],

[
'label' => 'quantity',
'value' => function($data) {
return $data->quantity;
}
],
['class' => 'yii\grid\ActionColumn'],
],
'toolbar' => [
['content' =>
Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['index'], ['data-pjax' => false, 'class' => 'btn btn-default', 'title' => 'Reset Grid'])
],
'{export}',
'{toggleData}'
],
'panel' => [
'heading' => '<i class="glyphicon glyphicon-align-left"></i>&nbsp;&nbsp;<b>Products List</b>',
'before' => '', //IMPORTANT
],
]);
?>

如果我使用上面的代码,我会得到这样的结果:

    product   |   quantity    |   

t-shirt | 2 |
t-shirt | 3 |
pants | 6 |

如有任何帮助,我们将不胜感激。谢谢

最佳答案

使用: http://www.yiiframework.com/doc-2.0/yii-data-sqldataprovider.html

并且在sql中使用group by, count ect...

然后在 GridView 列中这样使用:

[
'label' => 'product',
'value' => function($data) {
return $data['product'];
},
],

关于php - Yii2:将具有相同值的Sql数据显示到Kartik gridview中的一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41345617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com