• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help with session

I have been stuck on this session error for a day now and have tried a million different techniques to try and solve it.

Basically when the session is empty or a new request to the page there is no data in the session variable which means as I load the page it is giving the following errors:

Message: Undefined index: totalprice

Filename: main/cart.php

Line Number: 4,

Severity: Notice

Message: Undefined index: cart

Filename: main/cart.php,

Message: Undefined index: cart

Filename: controllers/welcome.php

Line Number: 79,

It is the code from the wrox book professional codeigniter, i have already read the posts on here but they do not answer my problem I have tried again and again but still nothing so someone please help on this one..

function cart($productid = -1) {
     if ($productid > 0){
      $fullproduct = $this->MCart->getProduct($productid);
       redirect('welcome/paving_stone/'.$productid, 'refresh');
       } else {
       $data['title'] = "Venus Stone | Shopping Cart";
        if (count($_SESSION['cart']) == true) {
         $data['cats'] = $this->MCats->getTopCategories();
         $data['main'] = 'cart';
      }  else  {
       $data['cats'] = $this->MCats->getTopCategories();
         $data['main'] = 'cart';
         $this->session->set_flashdata('conf_msg', "Your Cart Is Empty.");


function updateCart($productid,$fullproduct){
      $cart = $_SESSION['cart'];
      $totalprice = 0;
       if (count($fullproduct)) {
       if (isset($cart[$productid])) {
        $prevct = $cart[$productid]['count'];
        $prevname = $cart[$productid]['name'];
        $prevprice = $cart[$productid]['price'];
        $cart[$productid] = array(
         'name' => $prevname,
         'price' => $prevprice,
         'count' => $prevct + 1            );
           } else {
         $cart[$productid] = array(
         'name' => $fullproduct['name'],
         'price' => $fullproduct['price'],
         'count' => 1
         foreach ($cart as $id => $product){
           $totalprice += $product['price'] * $product['count'];
         $_SESSION['totalprice'] = $this->format_currency($totalprice);
         $_SESSION['cart'] = $cart;
         $this->session->set_flashdata('conf_msg', "We've added this product to your cart.");

So if anyone has a clue please enlighten me...

If you add a product to the cart the error then stops as it has started a valid session with the variables inside..

Not had a great deal of experience dealing with session data so if some kind person would point me in the right direction I would be ever so grateful...

Thanks in advance Paul

The proper way would be to check for the session variable before doing anything with it, just like you should with any other variable that may or may not be present.

if ($this->session->userdata('key') !== FALSE)
  // do something with session

However, you are using a mixture of CI's session class and PHP Native Sessions (WHY???), so you need to do the same type of thing except checking to see if your session variable isset() before trying to use it. This doesn't really have anything specific to do with CI, but rather trying to use PHP variables that may not be set.

Can you explain a little further I am new to php also, how would this apply to the current script?


$_SESSION is a PHP-variable. It is not CodeIgniter.

This is how CodeIgniter wants you to deal with session.

I understand its a global variable, but when I tried to add it into an if statement all i get is a foreach error which I cannot seem to rectify, hense why I asked for an example to demonstrate where I am going wrong. Forgive me if this sounds patronising but isn't that how people learn construct.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.