php - Yii2: sum of a column on filtered values in Gridview -
i can sum column in gridview using below code:
<?php $command = yii::$app->db->createcommand("select sum(net_total) estimate"); $sum = $command->queryscalar(); echo 'total ='. $sum; ?>
i have column in db discharge_date
(which date & time field) , want change sum on filter on column. if filtered data show 5 records, want sum 5 records. thanks.
updates per answer
the code this:
$query = app\models\estimate::find(); $dataprovider = new activedataprovider([ 'query' => $query, ]); $ids = []; foreach($dataprovider $i => $model) { $ids[] = $model->id;} $command = yii::$app->db->createcommand("select sum(net_total) estimate `id` in ('.implode(',',$ids).')"); // please use prepared statement instead, proof of concept $sum = $command->queryscalar(); echo $sum;
now getting error on line $ids[] = $model->id;}
getting unknown property: yii\db\activequery::id
did try adding query activedataprovider directly? specifying additional query parts possible explained here, should possible solution. alternatively try extract ids activedataprovider results , use them modify secondary query.
example second approach:
// should make sure same parameters passed // gridview's activedataprovider , model's find() function $idquery = estimate::find()->all(); $ids = []; foreach ($idquery $i => $model) { $ids[] = $model['id']; } $command = yii::$app->db->createcommand('select sum(net_total) estimate `id` in ('.implode(',',$ids).')'); // please use prepared statement instead, proof of concept $sum = $command->queryscalar();
Comments
Post a Comment