SitePoint Sponsor |
|
User Tag List
Results 1 to 1 of 1
Thread: Shopping Basket Problem
-
Jan 14, 2005, 16:56 #1
- Join Date
- Dec 2004
- Location
- Nottingham, UK
- Posts
- 22
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Shopping Basket Problem
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: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??
PHP Code:<?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)
{
//display what is in the shopping basket
$cart = $_SESSION['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)
{
$vinyl = get_vinyl_details($catno);
print '<tr>';
if($images == true)
{
print '<td align="left">';
if(file_exists("images/$catno.jpg"))
{
$size = GetImageSize('images/'. $catno. '.jpg');
if($size[0] > 0 && $size[1] > 0)
{
print '<img src="images/'. $catno. 'jpg" ';
print 'width='. $size[0]/3 .'height = '.$size[1]/3 . '>';
}
}
else
print ' ';
print '</td>';
}
print '<td align="left">';
print '<font color="#44802c" size="3" face="Arial, Helvetica, sans-serif"><a href="show_vinyls.php?cat_no='.$catno.'">'.$vinyl['title'].'</a> - '.$vinyl['artist_id'].'</font>';
print '</td><td align="center"><font color="#44802c" size="3" face="Arial, Helvetica, sans-serif">£'.number_format($vinyl['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['price']*$qty, 2).'</font></td></tr>';
}
//display of total row
echo '<tr>
<th colspan="'. (2+$images) .'" bgcolor="#7b9815"> </td>
<th align="center" bgcolor="#7b9815">'.$_SESSION['items'].'</th>
<th align="center" bgcolor="#7b9815">£'.number_format($_SESSION['total_price'], 2).'</th>
</tr>';
//offer save feature
if($change == true)
{
echo '<tr>
<td colspan="'. (2+$images) .'" </td>
<td align="center">
<input type="hidden" name="save" value="true">
<input type="submit" value="Save Changes" alt="Save Changes">
</td>
<td></td>
</tr>';
}
echo '</form></table>';
}
function calculate_price($cart)
{
// sum total price for all items in shopping cart
$price = 0.0;
$catno = $_SESSION['cat_no'];
if(is_array($cart))
{
$conn = db_connect();
foreach($cart as $isbn => $qty)
{
$query = "select price from vinyls where cat_no='$catno'";
$result = mysql_query($query);
if ($result)
{
$item_price = mysql_result($result, 0, 'price');
$price +=$item_price*$qty;
}
}
}
return $price;
}
function calculate_items($cart)
{
//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))
{
foreach($cart as $catno => $qty)
{
$items += $qty;
}
}
return $items;
}
?>
PHP Code:<?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['new'];
if($new_item)
{
//new item has been selected
if(!isset($_SESSION['cart']))
{
//If no new item added to the cart show cart details
$_SESSION['cart'] = array();
$_SESSION['items'] = 0;
$_SESSION['total_price'] = '0.00';
}
//If new item added then add this item to the cart
if(isset($_SESSION['cart'][$new_item]))
$_SESSION['cart'][$new_item]++;
else //caculate total price of cart using calculate_price function.
$_SESSION['cart'][$new_item] = 1;
$_SESSION['total_price'] =
calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);
echo 'Item '.$new_item.' added';
echo 'Session: '.$_SESSION['cart'].'';
}
if(isset($_SESSION['save']))
{
foreach ($_SESSION['cart'] as $catno => $qty)
{
if($_SESSION['cat_no']=='0')
unset($_SESSION['cart']['cat_no']);
else
$_SESSION['cart']['cat_no'] = $_SESSION['cat_no'];
}
$_SESSION['total_price'] =
calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);
}
print '<center><b>Your Shopping Basket</b></center><br />';
if($_SESSION['cart'] &&array_count_values($_SESSION['cart']))
display_cart($_SESSION['cart']);//don't work here
else
{
print '<p>There are no items in your shopping basket</p>';
}
$target = 'index.php';
//when item has been added to basket continue shopping in that genre.
if($new_item)
{
$details = get_vinyl_details($new_item);
if($details['genre_ref']);
}
print "<table>[<a href='show_genre.php?cat_no='".$details['cat_no']."'>Continue Shopping</a>]";
print "<br \>";
//this is for when i set up SSL
//$path = $HTTP_SESSION_VARS['PHP_SELF'];
//$server =$HTTP_SESSION_VARS['SERVER_NAME'];
//$path = str_replace('show_cart.php', '',$path);
//print 'https://'.$server.$path.'checkout.php','Go To Checkout');
//for none SSL
print "[<a href='checkout.php'>Checkout</a>]</table>";
do_html_footer();
?>
Bookmarks