I’m having trouble with this shopping cart tutorial I found on youtube, the prob is the $total, I try and edit it so it works but I can’t fix it, sometimes it takes the last product and doubles it and then adds the first one, heres the code where the problem is, Thanks.
[php]
function display_cart() {
//
$total =’ ‘;
foreach($SESSION as $prodid => $qntyinct ){
if($qntyinct>0){
if(substr($prodid, 0, 5)=='cart’){
$pid = substr($prodid, 5, (strlen($prodid)-5));
$q = mysql_query(‘SELECT id,title,price FROM products WHERE id=’.mysql_real_escape_string((int)$pid));
while($row = mysql_fetch_assoc($q)){
$sub = number_format($row[‘price’], 2) * $qntyinct;
echo $row[‘title’]." X
@ $".number_format($row[‘price’], 2)." = $".number_format($sub, 2)."
";
}
}
$total = ' ';
$total = $total + number_format($sub, 2);
}
}
if ($total==0) {
echo $total;
echo “Your cart is empty”;
}
else {
echo ‘
Total: $’.number_format($total, 2);
}
?>
//html code
<?php
}
?>
[/php]