473,503 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trouble with arrays

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
2 1827
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1545
by: LMachado1 | last post by:
I just started with php and I'm trying to make a simple interface as follows: - user is asked to input an integers, for example: how many students do you want to enter? - user is then shown a...
16
1331
by: scorpion53061 | last post by:
Well as some of you know I was using a tab control for a project I was building for my boss. Today he tells me that he wants: When he switches tabs to be able to switch back and see whatever...
4
4017
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
9
7316
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it....
1
2044
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
2
2262
by: goetzie | last post by:
I am using Python 2.4.1 and Numeric 23.8 and running on Windows XP. I am passing a Numeric array of strings (objects) to a C Extension module using the following python code: import Numeric...
1
2433
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
2
1191
by: ericstein81 | last post by:
I have tried to develop a code using arrays and void functions to take the current earnings of employees and using it to calculate their future income in ten years. This was performed by a 8% percent...
4
1401
by: nico3334 | last post by:
I'm having problems with my code concerning a mulitdimensional array. Basically, I'm trying to create an array (arrTotal) which contains all of the data from 2 arrays (arrOne & arrTwo). Basically,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.