Jump to content

basic shopping cart problem


juanc

Recommended Posts

Hi I'm trying to make a basic shopping based on the idea that an item selected get's pushed into an array and then that array get's saved into a session variable.

 

If I did this..

 

<?php

$shop = array("plasma TV","DVD recorder","mountain bike","coffee maker");
$_SESSION['cart'] = $shop;

?>

I'm able to access $_SESSION['cart'] with all the array values on a completely different page no problem. However I want to have a page listing items for sale then each time a customer clicks on an item that get's added to $_SESSION['cart'] and of course each time you are able to see the latest snapshot of the cart.

 

I wrote this myself.........

<?php

// 1st page

<a href=cart.php?id=1>Computer</a><br />
<a href=cart.php?id=2>CD collection</a>

// then on cart.php

if(!in_array($_GET['id'],$_SESSION['cart']))
{
$items_in_cart[] = $_SESSION['cart'];
$new_entry[] = $_GET['id'];
$all_the_items = array_merge($items_in_cart,$new_entry);
$all_the_items = $_SESSION['cart'];
}


?>

what basically happens is if I do a foreach loop on $_SESSION['cart'] it's fine...I see all the original items (Plasma TV etc) plus the new one selected....however if I were to leave the page and then re visit I only see the original items in the original $shop array ...and not the latest one to be selected.

 

Hope someone can help.

Link to comment
Share on other sites

chigley I've done that......it's just I haven't bothered entering that much detail onto what I've posted here...

 

To sum my question.........how do you keep inserting values into an array that stays persistent across different pages?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.