Shopping Basket Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Shopping Basket Problem

Post by mmc01ms »

I've created a shopping basket and sorted out a problem i had yesterday however the problem i have now is this output which i get back to my script

Code: Select all

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 8 in c:\program files\apache group\apache2\htdocs\shopping_basket_fns.php on line 97

Don't Know what it means have a fair idea but don't know how to fix it. Code for my shopping_basket functions and show_cart are below. Any 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;
			$conn = db_connect();
			foreach($cart as $isbn => $qty)
			&#123;  
				$query = "select price from vinyls where cat_no='$catno'";
				$result = mysql_query($query);
				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']);
		echo 'Item '.$new_item.' added';
		echo 'Session: '.$_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;
	
	print "<table>&#1111;<a href='show_genre.php?cat_no='".$details&#1111;'cat_no']."'>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();
	
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

first off, I'd suggest adding a debug print of the query used above it, followed by augmenting the query call to

Code: Select all

mysql_query(...) or die(mysql_error());
this should show you if any of them fail for whatever reason.

My next suggestion is adding mysql_free_result() into the mix, just in case mysql is running out of memory and isn't able to say just that.
Post Reply