Solved

PHP Associated Array - shopping cart

Posted on 2004-10-12
7
1,259 Views
Last Modified: 2013-11-29
I am setting up a basic shopping cart and at present I have the folowing code to store the product_id and quantity of those items which the customer has chosen to add to the basket:  

$product_details = array("qty" => $quantity, "productID" => $product_id);


if(isset($_SESSION['cart'][$product_id]))
{
   
    $_SESSION['cart'][$product_id]['qty']+=$product_details['qty'];
}
else
{

    $_SESSION['cart'][$product_id]=$product_details;
}

I know this may sound a silly question, but how do I access items that have been saved to the $_SESSION['cart'] array?

Basically, what I need to do is:

1. Loop though the array and recall the stored product ID's so that the corrasponding data can be extracted from the database

2. Delete an item

3. Update the quantity

Any help is greatly appreciated.

Thanks
0
Comment
Question by:rvr_1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
Navicerts earned 63 total points
ID: 12286643
I'm not sure what this is what you are looking for but here are a couple ways to access your array's....

Looping through an array...

I like to use the "foreach" for this, this will go through each part of your array and do something for each part (print, do some math, add)

foreach( $FieldG as $Field_Name )      
{      
//do something
}


You can also access each part directly with something like this...

Array[1] = 'One'
Array[2] = 'Two'
Array[3] = 'Three'

print($Array[2]);

will return >>  Two

Hope this helps

-Navicerts
0
 

Author Comment

by:rvr_1
ID: 12286768
Thanks for the response - I wil try and explain in a little more detail what I am trying to achieve.

I have the array $product_details into which a product_id and quantity value are added.

$product_details = array("qty" => $quantity, "productID" => $product_id);

I then search the associated array to see if there is a record corrasponding to the product id that the user wished to add to the basket.  If there is, the quantity is increased by 1, otherwise the contents of the array $product_details is added to the associated array.

if(isset($_SESSION['cart'][$product_id]))
{
   
    $_SESSION['cart'][$product_id]['qty']+=$product_details['qty'];
}
else
{

    $_SESSION['cart'][$product_id]=$product_details;
}

Therefore, I now have an aray $_SESSION['cart'] which is made up of a series of arrays - each holding the product id and quantity of items in the cart.

What I am having trouble with it accessing the data stored in the associated array.

0
 
LVL 3

Assisted Solution

by:gnudiff
gnudiff earned 62 total points
ID: 12287292
what kind of trouble then?

You want to know what product id's are there?
that is, as navicert suggested:

foreach ($_SESSION['cart'] as $prod_id => $prod_det)
{
   print 'i have product '.$prod_id.' with the following quantity: '.$prod_det['qty'];
}
0
 

Author Comment

by:rvr_1
ID: 12298908
Thanks Navicerts and gnudiff.

This is my first php project - appologies if my questions sound too simple.  I'm learning as I go along.

I've got the above code working so that when a customer reviews their cart, the information from the array is extracted, a search performed on the database and a complete summary is provided to the user.

What I am having problems with now is updating the quanity of each item in the basket.  Next to each item is a text box that is initially populated with the quantity value held in the array.  I wish to give the user the option to change the value of each of these items.  

When the update basket button is clicked, I wish to loop through the array and update the quantity value for each item to reflect the new quantities held in the text boxes..  

Can you give me some help with how I go about doing this please?  

Thanks!
0
 
LVL 7

Expert Comment

by:Navicerts
ID: 12299083
Have you tried using a form to do this?  

You could have a textbox where they enter the number and your "Update cart" button is your "submit" button.  

This can be done assuming that your DB supports update statements from php.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question