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
https://forums.phpfreaks.com/topic/56950-basic-shopping-cart-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.