【发布时间】:2015-11-21 17:08:24
【问题描述】:
我正在尝试做一个 Yii2 应用程序。
我在 mysql 中有“customerID”、“customerName”和“total”列。
我想向用户展示所选客户对用户的总价值。
例如。
Customer 1 = 100
Customer 2 = 250
Customer 3 = 300
Customer 1 = 300
Customer 3 = 500
所以。如果用户在我的下拉列表中选择客户 3 我想向用户展示 300+ 500 = 800。
我可以看到特定客户的总列数。 但我无法获得所选客户的总列数
我该怎么做?
这是我下面的代码。
<?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;
?>
【问题讨论】:
-
所以您想按客户分组?并为值做一个 sum()
-
是的,我可以按客户分组。但我想显示所选客户的总价值
-
与您的问题无关,但请使用字符串连接以外的其他技术编写
where条件:where(['customerName' => $chosen])之类的东西要好得多(而且您不会稍后获取 pwnd,因为有人利用了该 SQL 注入漏洞) -
好的。我会做的。谢谢@tarleb :)