Jump to content

Session variable


RynMan

Recommended Posts

Hey guys!

 

I'm trying to work through my shopping cart here and am having a problem.  Everytime I start up a new session (clear my cookies in firefox) and start testing, I get the following error....

 

Notice: Undefined variable: cart in C:\wamp\www\Adept\action_handle.inc.php  on line 16

 

It's pointing to my code here....

 

session_start();

$cart = $_SESSION['cart'];
$action = $_GET['action'];
$iquantity =  $_POST['iquantity'];

 

I'm guessing it's because the session has just started so naturally there will be nothing in the 'cart'.  How is this usually avoided?  Do we need to assign something to the cart to start with?  I'm trying not to do that, as my cart counts the number of instances a number (ID) appears....so I can't really have any values in there to start.

 

Thanks for any help!

Link to comment
Share on other sites

Do something like this:

<?php
session_start();

$cart = (isset($_SESSION['cart']))?$_SESSION['cart']:'';
$action = $_GET['action'];
$iquantity =  $_POST['iquantity'];
?>

 

This will assign a 0 character string to $cart if there is no 'cart' session variable. If there is one, $cart will be assigned it's value.

 

Ken

Link to comment
Share on other sites

Do something like this:

<?php
session_start();

$cart = (isset($_SESSION['cart']))?$_SESSION['cart']:'';
$action = $_GET['action'];
$iquantity =  $_POST['iquantity'];
?>

 

This will assign a 0 character string to $cart if there is no 'cart' session variable. If there is one, $cart will be assigned it's value.

 

Ken

 

Awesome!  Thanks Ken! :)

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.