2

I'm trying to develop system using Yii2 framework. I've data in my database, and I want to display all the data from products table. For example:

Products Table

---------------------------------------------------------------------
**id** | **product** | **color**   | **quantity**   |    **size**   |
  
  1    |    t-shirt  |     red     |       2        |     L         | 
  2    |    t-shirt  |     blue    |       3        |     M         |
  3    |    pants    |     black   |       6        |     M         |

and I want to display this data to kartik gridview in index.php. But I want to display data by product not id, so my expected result is like this my index.php

Expected Result

product   |   quantity    |   
  
 t-shirt  |      5        |   
 pants    |      6        | 

My expected result avoid, color and size. It's merge all same product into one column in gridview.

How I can get like that? This is my code in 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
    ],
]);
?>

and if I use above code, I got result like this:

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

Any help would be appreciated. Thanks

Community
  • 1
  • 1
Blackjack
  • 1,016
  • 1
  • 20
  • 51

1 Answers1

0

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

And in sql use group by, count ect...

Then in GridView columns use like that:

[
    'label' => 'product',
    'value' => function($data) {
        return $data['product'];
    },
],
marche
  • 1,756
  • 1
  • 14
  • 24
zakrzu
  • 485
  • 4
  • 9
  • I've try but I got nothing, and I've try to understand but I'm not sure that I truly understand your suggestion. – Blackjack Dec 29 '16 at 07:53
  • First create raw sql query like: select products.*, count(product.quantity) as product_quantity from products group by product – zakrzu Dec 29 '16 at 15:15
  • First create raw sql query like: select products.*, count(product.quantity) as product_quantity from products group by product use this query in SqlDataProvider and use SqlDataProvider instead of ActiveDataProvider next in index.php use in columns value from array not from object like: [ 'label' => 'product', 'value' => function($data) { return $data['product']; }, ], – zakrzu Dec 29 '16 at 15:18