qty and price in shopping cart does not totalize

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
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

qty and price in shopping cart does not totalize

Post by victor »

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;
}
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

No actually, because the function returns the price value, and this is assigned to session variables.

The code is not easy-readable. But i'm trying to find why price is not accumulated?

Are you sure, the problem is stem from code?
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

session variable does not totalise

Post by victor »

by the way, the codes are obtain from the book php and mysql, I don't think it is stem from code, some config must have been missing and i don't know what, could it be due to the extensions that should be used in the php.ini file?

from the book, it says that magic codes needs to be switched on and I've not, could this be the problem? if yes and how do u do it?
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

qty and price in shopping cart does not totalize

Post by victor »

ya i see...magic code problem..it is settled thanks
Post Reply