0

I have a grid view which is like this. The columns are calculated with the help of MySQL query and displayed accordingly.

    <?php
  $subquery = Adanalytics::find()->
            select('id,ad_id,date_event,max(cpc) cpclick,max(cpv) cpview,max(impression) impression,max(view) view,max(clicks) clicks,visitor_ip,publisher_id')->
            from('ad_analytics')->
            where(['publisher_id' =>  Yii::$app->user->identity->id ])->
            groupBy('ad_id,date_event,visitor_ip');
  $query=Adanalytics::find()->
        select('ad_id,date_event,sum(cpclick) total_click_cost,sum(cpview) total_view_cost,sum(impression) total_impression,sum(view) total_views,sum(clicks) total_clicks,publisher_id')->
        from(['t'=>$subquery])->
        groupBy('t.ad_id,t.date_event');
 ?>

    <?= GridView::widget([

    'dataProvider'=>new ActiveDataProvider([
      'query' => $query,
    ]),
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'ad_id',
        'total_impression',
        'total_views',
        'total_clicks',
        'total_click_cost',
        'total_view_cost',
        'date_event',


        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

I want to show a grand total column at the bottom of this grid view.

For example

 'grand_total_impression',
        'grand_total_views', //This is the sum of all displayed views
        'grand_total_clicks',//This is the sum of all displayed clicks
        'grand_total_click_cost',//THis is the sum of all displayed cost
        'grand_total_view_cost',//This is the sum of all displayed view cost

To do that I have code in my controller which is like this.

  public function actionIndex()
    {
        $searchModel = new Adanalytics2Search();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $grandTotal = [];
        foreach ($dataProvider->getModels() as  $value) {
          // var_dump($value);
          // exit();
            $grandTotal['total_click_cost'] += $value['total_click_cost'];
            $grandTotal['total_view_cost'] += $value['total_view_cost'];
        }
        //var_dump($dataProvider);
         return $this->render('index', [
             'searchModel' => $searchModel,
             'dataProvider' => $dataProvider,
             'grandTotal'  => $grandTotal,
         ]);
    }

But it is producing the error in at this way.

Undefined index: total_click_cost

Where am I going wrong? Or is there any other way to solve this problem?

3 Answers3

0

I strongly advise you to use katrik gridview. It handles page summary much better and implements a lot of other usages. Alternatively add 'showPageSummary' => true at your gridview to display columns summary.

manosk
  • 34
  • 1
  • 5
  • I am getting this error `Setting unknown property: yii\grid\GridView::showPageSummary` – arun webber May 01 '18 at 11:49
  • After installing Karthik grid view I am getting this error now 'showPageSummary' => false, `Calling unknown method: yii\grid\SerialColumn::renderPageSummaryCell()` – arun webber May 01 '18 at 12:02
  • I have enabled Karthik grid by this `'showFooter' => true` but the grand total column is just empty there is no value – arun webber May 04 '18 at 07:01
0
use kartik\grid\GridView;

// Create a panel layout for your GridView widget
echo GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true
]);

Similar question: Yii2: Kartik Gridview sum of a column in footer

manosk
  • 34
  • 1
  • 5
  • I have saw this question but the problem is that the actual page summery wont shows up it is only showing an empty footer without any value – arun webber May 07 '18 at 05:48
0

This has solved the problem anyway.

This in grid column.

            [
              'attribute'=>'total_impression',
              'pageSummary' => true
            ],

And in grid view

'showPageSummary' => true,