我卡住了,我的目标是获取隐藏在$ _SESSION ['cart']内的ProductID 然后在SQL查询中使用这些ID来计算总和
这是我的代码
public function getProductID(){
$keys = array_keys($_SESSION['cart']);
return $keys;
}
public function getShippingFee(){
$ids = $this->getProductID();
foreach($ids as $id){
$query = mysql_query("SELECT * FROM deliverycharges WHERE ProductID = $id");
$row = mysql_fetch_assoct($query);
$sum = $row['Cost'] + $row['Tax'];
return $sum;
}
}
我粘贴的第二个函数是错误的,我已经完成了几种类型的循环但是,我无法得到真正的总和...我的
答案 0 :(得分:6)
返回后,退出该功能,因此您需要先对所有内容求和,然后在总结一切后再返回:
public function getShippingFee(){
$ids = $this->getProductID();
foreach($ids as $id){
$query = mysql_query("SELECT * FROM deliverycharges WHERE ProductID = $id");
$row = mysql_fetch_assoc($query);
$sum += $row['Cost'] + $row['Tax'];
}
return $sum;
}
答案 1 :(得分:0)
您正在覆盖每个循环的sum值。使用+ =
public function getShippingFee() {
$ids = $this->getProductID();
$sum = 0;
foreach($ids as $id) {
$query = mysql_query("SELECT * FROM deliverycharges WHERE ProductID = $id");
$row = mysql_fetch_assoct($query);
$sum += $row['Cost'] + $row['Tax'];
}
return $sum;
}