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; ?>
wherecondition using some technique other than string concatenation: Something likewhere(['customerName' => $chosen])is much better (and you won't get pwnd later because someone exploited that SQL injection vulnerability)