0

I am trying to do a Yii2 application.

I have 'customerID', 'customerName' and 'total' column in mysql.

I want to show user to selected customers's total value to user.

For Example.

Customer 1 = 100 Customer 2 = 250 Customer 3 = 300 Customer 1 = 300 Customer 3 = 500 

So. If user choose Customer 3 in my dropdownlist I want to show user to 300+ 500 = 800.

I can see the sum of total columns for specific customer. But I cant get the sum of total columns of selected customer

How can I do this?

This is my code below.

<?php $form = ActiveForm::begin(); ?> <?php $chosen = ""; ?> <?= $form->field($model, 'customerName')->dropDownList( ArrayHelper::map(Siparisler::find() ->all(),'customerName','customerName'), [ 'prompt'=>'Chose a Customer' ] ); $var = ArrayHelper::map(Siparisler::find()->where("(customerName = '$chosen' )")->all(),'total','total'); echo "<h3><br>"."Total"."<br><h3>"; $sum = 0; foreach($var as $key=>$value) { $sum+= $value; } echo $sum; ?> 
4
  • so you want to group by customers? and do a sum() for the values Commented Nov 21, 2015 at 19:35
  • Yes I can group by customers. But I want to show the total value of selected customer Commented Nov 21, 2015 at 19:43
  • 1
    Unrelated to your question, but please write the where condition using some technique other than string concatenation: Something like where(['customerName' => $chosen]) is much better (and you won't get pwnd later because someone exploited that SQL injection vulnerability) Commented Nov 21, 2015 at 21:49
  • Ok. I will do it. thanks @tarleb :) Commented Nov 21, 2015 at 22:48

2 Answers 2

1

try this. These should be in your controller's action

public function actionTotal() { //you've use $chosen for selected customer in drop down list $chosen = Yii::$app->request->post('chosen', ''); // select all customer data based on $chosen $customers = Siparisler::find()->where(['=', 'customerName', $chosen]) ->all(); $sum = 0; foreach($customers as $k=>$customer) { $sum += $customer->total; } return $this->render('total', [ 'sum' => $sum, 'customers' => $customers, ]); } 

these code below should be your view

$form = ActiveForm::begin(); // i use yii\helpers\Html Html::dropDownList('chosen', ArrayHelper::map(Siparisler::find()->all(), 'customerName', 'customerName'), [ 'prompt'=>'Chose a Customer' ]); Html::submitButton('Submit'); ActiveForm::end(); echo "<h3><br>" . "Total" . "<br>" . $sum . "<h3>"; 
Sign up to request clarification or add additional context in comments.

2 Comments

Instead of doing $chosen = isset($post['chosen']) ? $post['chosen'] : '' you could do $chosen = Yii::$app->request->post('chosen', '')
The if statement wrapping the foreach loop isn't necessary and can be dropped without changing behavior.
0

In addition to David's answer: Summing over a column can also be done in pure SQL. This might save you from some unwanted PHP complexity (I try to avoid ArrayHelper::map whenever I can). The query for this would be

SELECT sum(total) as sumTotal FROM customer WHERE customerName = '<NAME>'; 

or in Yii2:

Siparisler::find()->where(['customerName' => $chosenName])->sum('total'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.