这表示 $total 和 $sub 对于 $total += $sub 是未定义的。$sub 是在 while 循环中声明的,并且两个 $sub 都在函数内,所以它应该是一个局部变量。为什么我不能使用它?
public function cart() {
foreach($_SESSION as $name=>$value){
if (substr($name, 0, 5) == 'cart_') {
if((int)$value > 0){
$id = substr($name, 5, (strlen($name)-5));
$st = $this->db->prepare("select id, name, price from deals where id=?");
$st->bindParam(1, $id);
$st->execute();
while($cart_item = $st->fetch(PDO::FETCH_OBJ)){
$sub = $cart_item->price*$value;
echo $cart_item->name.' x '.$value.' @ '.$cart_item->price.' = '.$sub.' <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?delete='.$id.'">[Delete]</a><br/>';
}
}
}
$total += $sub;
}
}