이것은 $ 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;
}
}
'$ st-> fetch (PDO :: FETCH_OBJ)'가 항목을 반환하지 않을 때 $ sub는 정의되지 않을 수 있습니다. – Neverever