Session array not updating - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Session array not updating

ejay56ejay56 Posts: 6Member
Hi ,

i am trying to build a simple online shopping cart. Been strugling with 2 dimensional sessions array and when i finally got it working the session array is not updating.

I want to be able to browse through each product page and keep adding the productid,quantity to $_session[cart].

But each time i add to cart the previous product ids and quantities are deleted from the session. Am i doing it correctly.

Somebody please help.Thanks in advance.

regards,Ejay56

Here is my code:
<?
foreach($_POST as $var => $value)
{
if($value != '')
{
// add to session
$_SESSION['cart'][$var] = $value;
}
}
?>
<?
foreach($_SESSION['cart'] as $pid => $qty)
{
echo "Product id: $pid, Quantity:$qty ";
echo "
";
}
?>

Comments

  • mac_doggiemac_doggie Posts: 488Member
    Hi,

    You'll need to call the session_start() function before you can use your $_SESSION array. You'll have to do this before you output anything, so you better do it all the way up front of your source. If you would do an acho before the session_start() you'll get an error like: output allready started at line ...

    [code]
    <?
    session_start();
    foreach($_POST as $var => $value) {
    if($value != '') {
    // add to session
    $_SESSION['cart'][$var] = $value;
    }
    }
    ?>
    <?
    session_start();
    foreach($_SESSION['cart'] as $pid => $qty) {
    echo "Product id: $pid, Quantity:$qty ";
    echo "
    ";
    }
    ?>
    [/code]
    ;-)
    -mac-
    mailto:[email protected]
    the Netherlands...
  • ejay56ejay56 Posts: 6Member
    Hi Mac,

    ahh..

    Thank you so much! its working perfectly fine now! Wasted the whole of yesterday trying to figure out this.

    -ejay56
Sign In or Register to comment.