PHP – Yii2 Gridview Footer – Sum of Column value

Here is some of the sample code to get the sum of the column values in the footer in the gridview. It was 100% working in the gridview in Yii2 and not tried in the Yii and hope it works in that too. All you need to change is the values

 [
 'label' => 'cost',
 'attribute' => 'cost',
 'value' => function ($model, $key, $index, $widget) {
 return $model->cost;
 },
 'footer' => $cost,
 ],

The above code should be added in the gridview with footer enabled.

'showFooter' =>true,

Here cost the value which need to be totaled, so you need to change according to your needs. You also need to add the below code in the view page.

Read Article :  How to Install Apache 2.x and PHP 7 in Windows Step by Step

<?php
$cost = 0;
if (!empty($dataProvider->getModels())) {
 foreach ($dataProvider->getModels() as $key => $val) {
     $cost += $val->cost;
    }
}
?>

Use this code and calculate sum of values in the footer in the Yii2 Gridview.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

nineteen − eleven =