Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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:mac_doggie@hotmail.com
    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.