Shopping Basket Problem
Posted: Mon Jan 17, 2005 2:26 pm
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)
{
//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))
{
$link_id = db_connect();
foreach($cart as $isbn => $qty)
{
$query = "select price from vinyls where cat_no='".$_GETї'new']."'";
$result = mysql_query($query, $link_id);
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;
}
?>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ї'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']);
}
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']);
}
$genre = $detailsї'genre_ref'];
print $catno;
print "<table>ї<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ї'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();
?>