I'm trying to create a mini shopping cart info box about how many items are in the cart and the total price. The code below works when the variables are zero, but when something is added in the cart they won't show and I can't figure out:why?
php function:
function a1() {
$num=0;
$pric3=0;
if(isset($_SESSION['cart'])){
$num=count($_SESSION['cart']);
if(isset($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $id => $qty) {
$query = 'SELECT price FROM proizvod WHERE id=' . $id;
$result = $this->db->query($query);
foreach ($result as $row) {
$pric=$qty*$row['price'];
$pric3=$pric3 +$pric;
$pric=0;
}
}
}
$a = array(
'num' =>$num,
'price'=>$pric3);
$json = json_encode($a);
echo $json;
}
}
and my ajax script:
$(document).ready(function(){
$.ajax({
url: 'ajax.php',
type: "post",
cache: false,
dataType: 'json',
success: function (data) {
$("#miniQty").html(data.num);
$("#miniPrice").html(data.price);
},
error: function (jqXHR, textStatus, errorThrown) {
$("#miniQty").html("");
$("#miniPrice").html("");
}
});
});