Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1275
  • Last Modified:

PHP Associated Array - shopping cart

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
rvr_1
Asked:
rvr_1
  • 2
  • 2
2 Solutions
 
NavicertsCommented:
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
 
rvr_1Author Commented:
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
 
gnudiffCommented:
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
 
rvr_1Author Commented:
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
 
NavicertsCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now