By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

trouble with arrays

P: n/a
Hi,

I'm trying to modify a shopping cart script from Mastering PHP/MySQL and am
having trouble setting up some arrays for it. The original code, below,
stores the cart items in a session variable array
($HTTP_SESSION_VARS['cart'] = array();) basically this stores an associative
array with the id number of the product and the qty (ie cart[$productid] =
qty of that product).

This works fine, but I need to be able to have sub options AND colours for
some (but not all) products. I don't know exactly what to do here, I know I
need some kind of array using a combination of the productid,colour and
option as the key for it. then storing the quantity of the item in that (and
if possible price).

Here is the code that sets up the session variables and updates the quantity
if an item is already in there (the array I'm trying to modify is the 'cart'
one, $new is the productid of the new item to add (and
$newcolour,$newoptions will be the colour and option for the new item)).

if($new) { //see if a product has been specified
if(!isset($HTTP_SESSION_VARS['cart'])) { //see if session variables
have been set
$HTTP_SESSION_VARS['cart'] = array(); //if not create them
$HTTP_SESSION_VARS['items'] = 0;
$HTTP_SESSION_VARS['total_price'] ='0.00';
}
if(isset($HTTP_SESSION_VARS['cart'][$new]{ //check if there is already
a value in cart array for that product
$HTTP_SESSION_VARS['cart'][$new]++; //if so increase quantity
}
else {
$HTTP_SESSION_VARS['cart'][$new] = 1; //otherwise just put one item
in
}
$HTTP_SESSION_VARS['total_price'] =
calculateprice($HTTP_SESSION_VARS['cart'],$dbproducts); //update total
price
$HTTP_SESSION_VARS['items'] = calculateitems($HTTP_SESSION_VARS['cart']);
//update total items
}
if(isset($save)) { //check if this is an
update
foreach ($HTTP_SESSION_VARS['cart'] as $productid => $qty) {
//go through each item in the cart (session)
if($HTTP_POST_VARS[$productid]=='0') { //check if
quantity set to 0
unset($HTTP_SESSION_VARS['cart'][$productid]); //if item
is removed then remove from cart array
}
else {
$HTTP_SESSION_VARS['cart'][$productid] = $HTTP_POST_VARS[$productid];
//change quantity
}
$HTTP_SESSION_VARS['total_price'] =
calculateprice($HTTP_SESSION_VARS['cart'],$dbproducts); //re claculate
price and no. items
$HTTP_SESSION_VARS['items'] = calculateitems($HTTP_SESSION_VARS['cart']);
}
}

Thanks for any help

Alex

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 21 Jun 2004 14:12:53 +0100, Alex Hopson wrote:
Thanks for any help


I'm not sure what you are doing, or specifically what you are asking. I
do, however, have a suggestion that will greatly simplify your work with
PHP session variables.

Use the $_SESSION array instead of the $HTTP_SESSION_VARS array!

See the following:

http://us3.php.net/manual/en/reserve...iables.session
http://us3.php.net/manual/en/ref.session.php
http://us3.php.net/variables.predefined

I mention this because a lot of tutorials, books, and articles about PHP 4
fail to mention the existence of the $_SESSION array because they are
outdated. However, this array has been available since PHP 4.1.x which is
over two years old!

allright, later...

(Clarify your original question if you want a more specific answer.)

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #2

P: n/a
Why don't you create an object/class with all the data you need and
store that in $_SESSION['cart'] instead of a multidimensional array.
It will be a little slower, but a lot easier to deal with imho.

Jay

"Alex Hopson" <al*********************@hotmail.com> wrote in message news:<j3*******************@fe26.usenetserver.com> ...
Hi,

I'm trying to modify a shopping cart script from Mastering PHP/MySQL and am
having trouble setting up some arrays for it. The original code, below,
stores the cart items in a session variable array
($HTTP_SESSION_VARS['cart'] = array();) basically this stores an associative
array with the id number of the product and the qty (ie cart[$productid] =
qty of that product).

This works fine, but I need to be able to have sub options AND colours for
some (but not all) products. I don't know exactly what to do here, I know I
need some kind of array using a combination of the productid,colour and
option as the key for it. then storing the quantity of the item in that (and
if possible price).

Here is the code that sets up the session variables and updates the quantity
if an item is already in there (the array I'm trying to modify is the 'cart'
one, $new is the productid of the new item to add (and
$newcolour,$newoptions will be the colour and option for the new item)).

if($new) { //see if a product has been specified
if(!isset($HTTP_SESSION_VARS['cart'])) { //see if session variables
have been set
$HTTP_SESSION_VARS['cart'] = array(); //if not create them
$HTTP_SESSION_VARS['items'] = 0;
$HTTP_SESSION_VARS['total_price'] ='0.00';
}
if(isset($HTTP_SESSION_VARS['cart'][$new]{ //check if there is already
a value in cart array for that product
$HTTP_SESSION_VARS['cart'][$new]++; //if so increase quantity
}
else {
$HTTP_SESSION_VARS['cart'][$new] = 1; //otherwise just put one item
in
}
$HTTP_SESSION_VARS['total_price'] =
calculateprice($HTTP_SESSION_VARS['cart'],$dbproducts); //update total
price
$HTTP_SESSION_VARS['items'] = calculateitems($HTTP_SESSION_VARS['cart']);
//update total items
}
if(isset($save)) { //check if this is an
update
foreach ($HTTP_SESSION_VARS['cart'] as $productid => $qty) {
//go through each item in the cart (session)
if($HTTP_POST_VARS[$productid]=='0') { //check if
quantity set to 0
unset($HTTP_SESSION_VARS['cart'][$productid]); //if item
is removed then remove from cart array
}
else {
$HTTP_SESSION_VARS['cart'][$productid] = $HTTP_POST_VARS[$productid];
//change quantity
}
$HTTP_SESSION_VARS['total_price'] =
calculateprice($HTTP_SESSION_VARS['cart'],$dbproducts); //re claculate
price and no. items
$HTTP_SESSION_VARS['items'] = calculateitems($HTTP_SESSION_VARS['cart']);
}
}

Thanks for any help

Alex

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.