Page 1 of 1

Shopping Basket Problem

Posted: Mon Jan 17, 2005 2:26 pm
by mmc01ms
I've created a shopping cart script. The only problem is that it doesn't seem to add up the right total for the amount in the cart. Can't see why not any one able to help?


Code: Select all

<?php
	
	
	//Script wrote with reference from Luke Wellings PHP and MySQL Web Development 2nd Edition Chapter 25 Page 532
	//shopping_basket_fns.php written by Manpreet Sandhu 12/01/2005
	
	
	require_once('news_fns.php');
	
	function display_cart($cart, $change = true, $images = 1)
	&#123;
		//display what is in the shopping basket
		
		$cart = $_SESSION&#1111;'cart'];
		
		
		echo '<table border="0" width="100%" cellspacing="0">
			<form action="show_cart.php" method="post">
			<tr><th colspan="'. (1+$images) .'" bgcolor="#7b9815">Item</th>
			<th bgcolor="#7b9815">Price</th><th bgcolor="#7b9815">Qty</th>
			<th bgcolor="#7b9815">Total</th></tr>';
			
			//display each item as a table row
			
			foreach ($cart as $catno => $qty)
		&#123;
			$vinyl = get_vinyl_details($catno);
			print '<tr>';
			if($images == true)
			&#123;
				print '<td align="left">';
				if(file_exists("images/$catno.jpg"))
				&#123;
					$size = GetImageSize('images/'. $catno. '.jpg');
					if($size&#1111;0] > 0 && $size&#1111;1] > 0)
					&#123;
						print '<img src="images/'. $catno. 'jpg" ';
						print 'width='. $size&#1111;0]/3 .'height = '.$size&#1111;1]/3 . '>';
					&#125;
				&#125;
				else
					print '&nbsp;';
				print '</td>';
			&#125;
			print '<td align="left">';
			print '<font color="#44802c" size="3" face="Arial, Helvetica, sans-serif"><a href="show_vinyls.php?cat_no='.$catno.'">'.$vinyl&#1111;'title'].'</a> - '.$vinyl&#1111;'artist_id'].'</font>';
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price'], 2).'</font>';
			print '</td><td align="center">';
			
			//allowing change of qty
			
			if($change == true)//if change is true then show input box for qty
				echo'<input type="text" name="'.$catno.'" value="'.$qty.'" size="1">';
			else
				echo $qty;//else just show qty and total price
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price']*$qty, 2).'</font></td></tr>';
			
		&#125;
		
		//display of total row
		
		echo '<tr>
					<th colspan="'. (2+$images) .'" bgcolor="#7b9815">&nbsp;</td>
					<th align="center" bgcolor="#7b9815">'.$_SESSION&#1111;'items'].'</th>
					<th align="center" bgcolor="#7b9815">£'.number_format($_SESSION&#1111;'total_price'], 2).'</th>
			 </tr>';
			
			//offer save feature
			
			if($change == true)
			&#123;
				echo '<tr>
						<td colspan="'. (2+$images) .'"&nbsp;</td>
						<td align="center">
						<input type="hidden" name="save" value="true">
						<input type="submit" value="Save Changes" alt="Save Changes">
						</td>
						<td></td>
					 </tr>';
				&#125;
			echo '</form></table>';
	&#125;
	
	function calculate_price($cart)
	&#123;
		// sum total price for all items in shopping cart
		$price = 0.0;
		$catno = $_SESSION&#1111;'cat_no'];
		if(is_array($cart))
		&#123;
			$link_id = db_connect();
			foreach($cart as $isbn => $qty)
			&#123;  
				$query = "select price from vinyls where cat_no='".$_GET&#1111;'new']."'";
				$result = mysql_query($query, $link_id);
				if ($result)
				&#123;
					$item_price = mysql_result($result, 0, 'price');
					
					$price +=$item_price*$qty;
				&#125;
			&#125;
		&#125;
		return $price;
	&#125;
	
	
	function calculate_items($cart)
	&#123;
		//total items in the cart which goes through the cart and adds up the quantities of each item to get the total number og items
		
		$items = 0;
		if(is_array($cart))
		&#123;
			foreach($cart as $catno => $qty)
			&#123;
				$items += $qty;
			&#125;
		&#125;
		return $items;
	&#125;
?>

Code: Select all

<?php
	
	session_start(); //must start a session for cart

	
	//Script written with reference from Luke Wellings PHP and MySQL Web Development 2nd Edition Chapter 25 Page 529
	//show_cart.php written by Manpreet Sandhu 12/01/2005
	require('page.inc'); //page class	
	require_once('shopping_basket_fns.php');
	
	
	$shoppingCart = new Page(); //Creating new Page Class to create layout of page
	
	$shoppingCart -> Display();
	
	display_main_menu(); //display side bar menu
	
	
	$new_item = $_GET&#1111;'new'];
	
	if($new_item)
	&#123;
		//new item has been selected
		if(!isset($_SESSION&#1111;'cart']))
		&#123;
			//If no new item added to the cart show cart details
			$_SESSION&#1111;'cart'] = array();
			$_SESSION&#1111;'items'] = 0;
			$_SESSION&#1111;'total_price'] = '0.00';
			
			
		&#125;
		
		//If new item added then add this item to the cart
		
		if(isset($_SESSION&#1111;'cart']&#1111;$new_item]))
			$_SESSION&#1111;'cart']&#1111;$new_item]++;
		else //caculate total price of cart using calculate_price function.
			$_SESSION&#1111;'cart']&#1111;$new_item] = 1;
			$_SESSION&#1111;'total_price'] =
				calculate_price($_SESSION&#1111;'cart']);
		$_SESSION&#1111;'items'] = calculate_items($_SESSION&#1111;'cart']);
	&#125;
	
	if(isset($_SESSION&#1111;'save']))
	&#123;
		foreach ($_SESSION&#1111;'cart'] as $catno => $qty)
		&#123;
			if($_SESSION&#1111;'cat_no']=='0')
				unset($_SESSION&#1111;'cart']&#1111;'cat_no']);
			else
				$_SESSION&#1111;'cart']&#1111;'cat_no'] = $_SESSION&#1111;'cat_no'];
		&#125;
		$_SESSION&#1111;'total_price'] =
			calculate_price($_SESSION&#1111;'cart']);
		$_SESSION&#1111;'items'] = calculate_items($_SESSION&#1111;'cart']);
	&#125;
	
	print '<center><b>Your Shopping Basket</b></center><br />';
	
	if($_SESSION&#1111;'cart'] &&array_count_values($_SESSION&#1111;'cart']))
		display_cart($_SESSION&#1111;'cart']);//don't work here
	else
	&#123;
		print '<p>There are no items in your shopping basket</p>';
		
	&#125;
	
	$target = 'index.php';
	
	//when item has been added to basket continue shopping in that genre.
	if($new_item)
	&#123;
		$details = get_vinyl_details($new_item);
		if($details&#1111;'genre_ref']);
	&#125;
	
	$genre = $details&#1111;'genre_ref'];
	
	print $catno;
	
	print "<table>&#1111;<a href='show_genre.php?genre_ref=".$genre."'>Continue Shopping</a>]";
	print "<br \>";
	
	//this is for when i set up SSL
	//$path = $HTTP_SESSION_VARS&#1111;'PHP_SELF'];
	//$server =$HTTP_SESSION_VARS&#1111;'SERVER_NAME'];
	//$path = str_replace('show_cart.php', '',$path);
	//print 'https://'.$server.$path.'checkout.php','Go To Checkout');
		
		//for none SSL
		
		print "&#1111;<a href='checkout.php'>Checkout</a>]</table>";
	
	
	do_html_footer();
	
?>

Posted: Mon Jan 17, 2005 2:34 pm
by feyd
looks like the logic used in calculate_price() is teh funky.

Posted: Mon Jan 17, 2005 3:51 pm
by mmc01ms
what do u mean exactly? can anyone come up with a better way to calculate total price?


code amanded is


Code: Select all

<?php
	
	
	//Script wrote with reference from Luke Wellings PHP and MySQL Web Development 2nd Edition Chapter 25 Page 532
	//shopping_basket_fns.php written by Manpreet Sandhu 12/01/2005
	
	
	require_once('news_fns.php');
	
	function display_cart($cart, $change = true, $images = 1)
	&#123;
		//display what is in the shopping basket
		
		$cart = $_SESSION&#1111;'cart'];
		
		
		echo '<table border="0" width="100%" cellspacing="0">
			<form action="show_cart.php" method="post">
			<tr><th colspan="'. (1+$images) .'" bgcolor="#7b9815">Item</th>
			<th bgcolor="#7b9815">Price</th><th bgcolor="#7b9815">Qty</th>
			<th bgcolor="#7b9815">Total</th></tr>';
			
			//display each item as a table row
			
			foreach ($cart as $catno => $qty)
		&#123;
			$vinyl = get_vinyl_details($catno);
			print '<tr>';
			if($images == true)
			&#123;
				print '<td align="left">';
				if(file_exists("images/$catno.jpg"))
				&#123;
					$size = GetImageSize('images/'. $catno. '.jpg');
					if($size&#1111;0] > 0 && $size&#1111;1] > 0)
					&#123;
						print '<img src="images/'. $catno. 'jpg" ';
						print 'width='. $size&#1111;0]/3 .'height = '.$size&#1111;1]/3 . '>';
					&#125;
				&#125;
				else
					print '&nbsp;';
				print '</td>';
			&#125;
			print '<td align="left">';
			print '<font color="#44802c" size="3" face="Arial, Helvetica, sans-serif"><a href="show_vinyls.php?cat_no='.$catno.'">'.$vinyl&#1111;'title'].'</a> - '.$vinyl&#1111;'artist_id'].'</font>';
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price'], 2).'</font>';
			print '</td><td align="center">';
			
			//allowing change of qty
			
			if($change == true)//if change is true then show input box for qty
				echo'<input type="text" name="'.$catno.'" value="'.$qty.'" size="1">';
			else
				echo $qty;//else just show qty and total price
			print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl&#1111;'price']*$qty, 2).'</font></td></tr>';
			
		&#125;
		
		//display of total row
		
		echo '<tr>
					<th colspan="'. (2+$images) .'" bgcolor="#7b9815">&nbsp;</td>
					<th align="center" bgcolor="#7b9815">'.$_SESSION&#1111;'items'].'</th>
					<th align="center" bgcolor="#7b9815">£'.number_format($_SESSION&#1111;'total_price'], 2).'</th>
			 </tr>';
			
			//offer save feature
			
			if($change == true)
			&#123;
				echo '<tr>
						<td colspan="'. (2+$images) .'"&nbsp;</td>
						<td align="center">
						<input type="hidden" name="save" value="true">
						<input type="submit" value="Save Changes" alt="Save Changes">
						</td>
						<td></td>
					 </tr>';
				&#125;
			echo '</form></table>';
	&#125;
	
	function calculate_price($cart)
	&#123;
		// sum total price for all items in shopping cart
		$price = 0.0;
		if(is_array($cart))
		&#123;
			$link_id = db_connect();
			foreach($cart as $catno => $qty)
			&#123;  
				$query = "select price from vinyls where cat_no='".$_GET&#1111;'new']."'";
				$result = mysql_query($query, $link_id);
				if ($result)
				&#123;
					//$row = mysql_fetch_row($result);
					$item_price = mysql_result($result, 0, 'price');
					//$item_price = $row&#1111;'price'];
					
					$price +=$item_price*$qty;
				&#125;
			&#125;
		&#125;
		return $price;
	&#125;
	
	
	function calculate_items($cart)
	&#123;
		//total items in the cart which goes through the cart and adds up the quantities of each item to get the total number og items
		
		$items = 0;
		if(is_array($cart))
		&#123;
			foreach($cart as $catno => $qty)
			&#123;
				$items += $qty;
			&#125;
		&#125;
		return $items;
	&#125;
?>

Posted: Mon Jan 17, 2005 3:59 pm
by magicrobotmonkey
where is $_GET['new'] in the query in calculate_price() being set? its looks like you are looking up the same item over and over. I think you want to use $catno in the place of $_GET['new']

Posted: Mon Jan 17, 2005 4:13 pm
by feyd
my point exactly, magicrobotmonkey.. teh funky. ;)

Posted: Mon Jan 17, 2005 5:11 pm
by mmc01ms
cheers guys that did the trick.