Trouble with Shopping Cart Tut

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]

I do not see anything wrong really apart from the $total

[php]
$total = ’ ';
$total = $total + number_format($sub, 2);
[/php]

if the $total var is empty the you are adding nothing to the sub ?
so shouldn’t it be
[php]$total = number_format($sub, 2);[/php]

As it stands $total does nothing apart from see if the value is null

yeah, I tried that , because [php]<? $total = 0;[/php] didn’t work, and when I try what you said I got undefined $total. I still haven’t figured it out. Thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service