php - Yii2 Sum of a Column Based on Filter Option -
i have gridview
, 6 filter options placed on radio buttons. when click filter, gridview
change based on filter being clicked. here's picture:
now, want total converted amount
of table based on filter chosen. example, if choose draft
filter, table show draft
reimbursements , sum converted amount
, total placed @ top right side above table shown in photo.
i tried googling solutions 1 find no answer. here's snippet of code in controller:
$refreshdata = false; if (isset(yii::$app->request->getbodyparams()['sortby'])) { $refreshdata = true; $sortby = yii::$app->request->getbodyparams()['sortby']; } else { $sortby = 'show all'; $dataprovider = new activedataprovider([ 'query' => reimbursement::find()->where(['company_id' => new \mongoid($session['company_id'])]) ]); } // reimbursements $reimbursements = reimbursement::findall(['company_id' => new \mongoid($session['company_id'])]); if (isset(yii::$app->request->getbodyparams()['sortby'])) { if(yii::$app->request->getbodyparams()['sortby'] != '' && yii::$app->request->getbodyparams()['sortby'] != 'show all') { $refreshdata = true; $reimbursements = reimbursement::findall(['company_id' => new \mongoid($session['company_id']), 'status' => yii::$app->request->getbodyparams()['sortby']]); // $ids = []; // foreach ($reimbursements $i => $model) { // $ids[] = $model['_id']; // } // $command = yii::$app->db->createcommand('select sum(total) estimate `_id` in ('.implode(',',$ids).')'); // please use prepared statement instead, proof of concept // $sum = $command->queryscalar(); } elseif (yii::$app->request->getbodyparams()['sortby'] == 'show all') { $refreshdata = true; } } $dataprovider = new arraydataprovider([ 'allmodels' => $reimbursements, ]); if($refreshdata) { return $this->renderpartial('_index', [ 'dataprovider' => $dataprovider, ]); } $dataprovider->pagination->pagesize = 10; return $this->render('index', [ 'searchmodel' => $searchmodel, 'dataprovider' => $dataprovider, 'rmodel' => $rmodel, ]);
as may have noticed commented lines of code below if(yii::$app->request->getbodyparams()['sortby'] != '' && yii::$app->request->getbodyparams()['sortby'] != 'show all')
condition. tried 1 , doesn't work.
how implement this?
$reimbursements = reimbursement::find()->where(['company_id' => new \mongoid($session['company_id'])]); if (isset(yii::$app->request->getbodyparams()['sortby'])) { $refreshdata = true; $sortby = yii::$app->request->getbodyparams()['sortby']; $reimbursements->andwhere(['status' => yii::$app->request->getbodyparams()['sortby']]); } else { $refreshdata = false; $sortby = 'show all'; } $dataprovider = new activedataprovider([ 'query' => $reimbursements, ]); $totalsum = $reimbursements->sum('total');
Comments
Post a Comment