2016-10-13 3 views
0

Я делаю приложение учета затрат.вычислить среднее значение в yii2

Я хочу найти среднюю цену за 3 месяца.

Резюме, составленное в Kartik GridView, показывает начальную цену акций, а не общую сумму.

это мой контроллер

<?php 
 

 
namespace backend\controllers; 
 

 
use Yii; 
 
use backend\models\Triwulan; 
 
use backend\models\TriwulanSearch; 
 
use yii\web\Controller; 
 
use yii\web\NotFoundHttpException; 
 
use yii\filters\VerbFilter; 
 

 
/** 
 
* TriwulanController implements the CRUD actions for Triwulan model. 
 
*/ 
 
class TriwulanController extends Controller 
 
{ 
 
    /** 
 
    * @inheritdoc 
 
    */ 
 
    public function behaviors() 
 
    { 
 
     return [ 
 
      'verbs' => [ 
 
       'class' => VerbFilter::className(), 
 
       'actions' => [ 
 
        'delete' => ['POST'], 
 
       ], 
 
      ], 
 
     ]; 
 
    } 
 

 
    /** 
 
    * Lists all Triwulan models. 
 
    * @return mixed 
 
    */ 
 
    public function actionIndex() 
 
    { 
 
     $searchModel = new TriwulanSearch(); 
 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
 

 
     return $this->render('index', [ 
 
      'searchModel' => $searchModel, 
 
      'dataProvider' => $dataProvider, 
 
     ]); 
 
    } 
 

 
    /** 
 
    * Displays a single Triwulan model. 
 
    * @param string $id 
 
    * @return mixed 
 
    */ 
 
    public function actionView($id) 
 
    { 
 
     return $this->render('view', [ 
 
      'model' => $this->findModel($id), 
 
     ]); 
 
    } 
 

 
    /** 
 
    * Creates a new Triwulan model. 
 
    * If creation is successful, the browser will be redirected to the 'view' page. 
 
    * @return mixed 
 
    */ 
 
    public function actionCreate() 
 
    { 
 
     $model = new Triwulan(); 
 

 
     if ($model->load(Yii::$app->request->post()) && $model->save()) { 
 
      return $this->redirect(['view', 'id' => $model->rm_code]); 
 
     } else { 
 
      return $this->render('create', [ 
 
       'model' => $model, 
 
      ]); 
 
     } 
 
    } 
 

 
    /** 
 
    * Updates an existing Triwulan model. 
 
    * If update is successful, the browser will be redirected to the 'view' page. 
 
    * @param string $id 
 
    * @return mixed 
 
    */ 
 
    public function actionUpdate($id) 
 
    { 
 
     $model = $this->findModel($id); 
 

 
     if ($model->load(Yii::$app->request->post()) && $model->save()) { 
 
      return $this->redirect(['view', 'id' => $model->rm_code]); 
 
     } else { 
 
      return $this->render('update', [ 
 
       'model' => $model, 
 
      ]); 
 
     } 
 
    } 
 

 
    /** 
 
    * Deletes an existing Triwulan model. 
 
    * If deletion is successful, the browser will be redirected to the 'index' page. 
 
    * @param string $id 
 
    * @return mixed 
 
    */ 
 
    public function actionDelete($id) 
 
    { 
 
     $this->findModel($id)->delete(); 
 

 
     return $this->redirect(['index']); 
 
    } 
 

 
    /** 
 
    * Finds the Triwulan model based on its primary key value. 
 
    * If the model is not found, a 404 HTTP exception will be thrown. 
 
    * @param string $id 
 
    * @return Triwulan the loaded model 
 
    * @throws NotFoundHttpException if the model cannot be found 
 
    */ 
 
    protected function findModel($id) 
 
    { 
 
     if (($model = Triwulan::findOne($id)) !== null) { 
 
      return $model; 
 
     } else { 
 
      throw new NotFoundHttpException('The requested page does not exist.'); 
 
     } 
 
    } 
 
}

это мой взгляд

<?php 
 

 
use yii\helpers\Html; 
 
use yii\widgets\DetailView; 
 
use kartik\grid\GridView; 
 

 

 
/* @var $this yii\web\View */ 
 
/* @var $model backend\models\Triwulan */ 
 

 
$this->title = $model->rm_code; 
 
$this->params['breadcrumbs'][] = ['label' => 'Triwulans', 'url' => ['index']]; 
 
$this->params['breadcrumbs'][] = $this->title; 
 
?> 
 
<div class="triwulan-view"> 
 

 
    <h1><?= Html::encode($this->title) ?></h1> 
 

 

 
    <?= DetailView::widget([ 
 
     'model' => $model, 
 
     'attributes' => [ 
 
      'rm_code', 
 
      'deskripsi_barang', 
 
     ], 
 
    ]) ?> 
 

 
</div> 
 
<?= GridView::widget([ 
 

 
     'dataProvider'=>new yii\data\ActiveDataProvider([ 
 

 
      'pagination'=>false, 
 
      'query'=>$model->getPenerimaans(), 
 

 
     ]), 
 
     'columns'=>[ 
 
     ['class' => 'kartik\grid\SerialColumn'], 
 

 
      'rm_code', 
 

 
       'bulan', 
 
      // 'price', 
 

 
       [ 
 
       'label' => 'Price', 
 
       //'attribute' => 'idDhs.idMatakuliah.jam', 
 
       'pageSummary' => true, 
 
       // 'pageSummary' => 'Total', 
 
       'value' => function ($model) { 
 
        if ($model) 
 
         return $model->price/3; 
 
       } 
 
       ], 
 
     //  ['class' => 'kartik\grid\ActionColumn'], 
 

 
     // 'product', 
 
      // 'qty' 
 
     ], 
 
     'showPageSummary' => true, 
 

 
    ]) ?> 
 

 
</div>

+0

Update вам вопрос и показать свой соответствующий код контроллера/действия .. Plaese – scaisEdge

+0

я сделал, чтобы обновить мой код @scaisEdge, пожалуйста, помогите мне –

ответ

0

исправляющие некоторые синтаксические ошибки от scaisEdge в ответ

public function actionIndex() 
{ 
    $searchModel = new TriwulanSearch(); 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 


    // calc the average 
    $myAverage = 0; 
    $myTot =0; 
    $myCnt = 0; 
    foreach ($dataProvider->models as $key => $value) { 
     $myTot += $value['price']; 
     $cnt++; // This should be $myCnt 
    } 

    if ($cnt>0){ // inside if use $myCnt 
     $myAverage = myTot/$myCont // here use $myTot/$myCnt 

    } 


    return $this->render('index', [ 
     'searchModel' => $searchModel, 
     'dataProvider' => $dataProvider, 

     // render the avegare too 
     'myAverage' => $myAverage, 
    ]); 
} 
+0

нормально @Nitin я хочу попробовать –

+0

спасибо н но эта ошибка уже была решена – scaisEdge

+0

@scaisEdge: добро пожаловать .. !! Я просто очистил синтаксические ошибки, которые вы не обновили в своем ответе, и из комментариев «Путра Пратамы», похоже, что он получает ответ как ноль. означает, что ошибка еще не решена. –

Смежные вопросы