qty and price in shopping cart does not totalize
Posted: Fri Feb 13, 2004 1:36 am
I'm an amatuer in php, currently applying the examples from a text. With database created and connection established, the qty and price doesn't seems to increase upon subsequent addition of new items.
The author did not make the price nor qty as session variables but return it and assign it to a session variable as a result of a function (i.e. each time a new item's price is retrieved from the database(see functions below), could this be a stateless problem for the accumulated total price in the function?
<?php
include ('book_sc_fns.php');
// The shopping cart needs sessions, so start one
session_start();
@ $new = $HTTP_GET_VARS['new'];
if($new)
{
//new item selected
if(!isset($HTTP_SESSION_VARS['cart']))
{
$HTTP_SESSION_VARS['cart'] = array();
$HTTP_SESSION_VARS['items'] = 0;
$HTTP_SESSION_VARS['total_price'] ='0.00';
}
if(isset($HTTP_SESSION_VARS['cart'][$new]))
{
$HTTP_SESSION_VARS['cart'][$new]++;
}
else
$HTTP_SESSION_VARS['cart'][$new] = 1;
$HTTP_SESSION_VARS['total_price'] = calculate_price ($HTTP_SESSION_VARS['cart']);
$HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
}
if(isset($HTTP_POST_VARS['save']))
{
foreach ($HTTP_SESSION_VARS['cart'] as $isbn => $qty)
{
if($HTTP_POST_VARS[$isbn]=='0')
unset($HTTP_SESSION_VARS['cart'][$isbn]);
else
$HTTP_SESSION_VARS['cart'][$isbn] = $HTTP_POST_VARS[$isbn];
}
$HTTP_SESSION_VARS['total_price'] =
calculate_price($HTTP_SESSION_VARS['cart']);
$HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
}
do_html_header('Your shopping cart');
if($HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart']))
display_cart($HTTP_SESSION_VARS['cart']);
else
{
echo '<p>There are no items in your cart</p>';
echo '<hr />';
}
$target = 'index.php';
// if we have just added an item to the cart, continue shopping in that category
if($new)
{
$details = get_book_details($new);
if($details['catid'])
$target = 'show_cat.php?catid='.$details['catid'];
}
display_button($target, 'continue-shopping', 'Continue Shopping');
//use this if SSL is set up
//$path = $HTTP_SERVER_VARS['PHP_SELF'];
//$server = $HTTP_SERVER_VARS['SERVER_NAME'];
//$path = str_replace('show_cart.php', '', $path);
//display_button('https://'.$server.$path.'checkout.php','go-to-checkout', 'Go To Checkout');
// if no SSL use below code
display_button('checkout.php', 'go-to-checkout', 'Go To Checkout');
do_html_footer();
?>
The functions for accumulation:
function calculate_price($cart)
{
// sum total price for all items in shopping cart
$price = 0.0;
if(is_array($cart))
{
$conn = db_connect();
foreach($cart as $isbn => $qty)
{
$query = "select price from books where isbn='$isbn'";
$result = mysql_query($query);
if ($result)
{
$item_price = mysql_result($result, 0, 'price');
$price +=$item_price*$qty;
}
}
}
return $price;
}
function calculate_items($cart)
{
// sum total items in shopping cart
$items = 0;
if(is_array($cart))
{
foreach($cart as $isbn => $qty)
{
$items += $qty;
}
}
return $items;
}
The author did not make the price nor qty as session variables but return it and assign it to a session variable as a result of a function (i.e. each time a new item's price is retrieved from the database(see functions below), could this be a stateless problem for the accumulated total price in the function?
<?php
include ('book_sc_fns.php');
// The shopping cart needs sessions, so start one
session_start();
@ $new = $HTTP_GET_VARS['new'];
if($new)
{
//new item selected
if(!isset($HTTP_SESSION_VARS['cart']))
{
$HTTP_SESSION_VARS['cart'] = array();
$HTTP_SESSION_VARS['items'] = 0;
$HTTP_SESSION_VARS['total_price'] ='0.00';
}
if(isset($HTTP_SESSION_VARS['cart'][$new]))
{
$HTTP_SESSION_VARS['cart'][$new]++;
}
else
$HTTP_SESSION_VARS['cart'][$new] = 1;
$HTTP_SESSION_VARS['total_price'] = calculate_price ($HTTP_SESSION_VARS['cart']);
$HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
}
if(isset($HTTP_POST_VARS['save']))
{
foreach ($HTTP_SESSION_VARS['cart'] as $isbn => $qty)
{
if($HTTP_POST_VARS[$isbn]=='0')
unset($HTTP_SESSION_VARS['cart'][$isbn]);
else
$HTTP_SESSION_VARS['cart'][$isbn] = $HTTP_POST_VARS[$isbn];
}
$HTTP_SESSION_VARS['total_price'] =
calculate_price($HTTP_SESSION_VARS['cart']);
$HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']);
}
do_html_header('Your shopping cart');
if($HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart']))
display_cart($HTTP_SESSION_VARS['cart']);
else
{
echo '<p>There are no items in your cart</p>';
echo '<hr />';
}
$target = 'index.php';
// if we have just added an item to the cart, continue shopping in that category
if($new)
{
$details = get_book_details($new);
if($details['catid'])
$target = 'show_cat.php?catid='.$details['catid'];
}
display_button($target, 'continue-shopping', 'Continue Shopping');
//use this if SSL is set up
//$path = $HTTP_SERVER_VARS['PHP_SELF'];
//$server = $HTTP_SERVER_VARS['SERVER_NAME'];
//$path = str_replace('show_cart.php', '', $path);
//display_button('https://'.$server.$path.'checkout.php','go-to-checkout', 'Go To Checkout');
// if no SSL use below code
display_button('checkout.php', 'go-to-checkout', 'Go To Checkout');
do_html_footer();
?>
The functions for accumulation:
function calculate_price($cart)
{
// sum total price for all items in shopping cart
$price = 0.0;
if(is_array($cart))
{
$conn = db_connect();
foreach($cart as $isbn => $qty)
{
$query = "select price from books where isbn='$isbn'";
$result = mysql_query($query);
if ($result)
{
$item_price = mysql_result($result, 0, 'price');
$price +=$item_price*$qty;
}
}
}
return $price;
}
function calculate_items($cart)
{
// sum total items in shopping cart
$items = 0;
if(is_array($cart))
{
foreach($cart as $isbn => $qty)
{
$items += $qty;
}
}
return $items;
}