Welcome Guest, Not a member yet? Register   Sign In
code igniter sessions not working with cart class
#1

[eluser]dazamate[/eluser]
Hi guys,

I am following the 'build a shopping cart' tutorial with CI by nettuts. I haven't even gotten into the first 5 minutes and I've got a problem.

It appears my sessions system isn't working and passing the data across. I did some reading and I see CI does not use normal php sessions but stores them in the database using the session ID as the identifier.

So I checked, all my database settings are right.

I am auto loading

Code:
$autoload['libraries'] = array('database', 'cart');
$autoload['helper'] = array('form', 'html', 'url');

I set this

Code:
$config['encryption_key'] = '';

^ my encryption key is made out of letters and a number, if that's alright to use.

I seen this was set to false so I set it to true

Code:
$config['sess_use_database'] = TRUE;


Here is the start of the cart controller...

Code:
class dazcart extends CI_Controller

{
  //adds item to shopping cart
  
  function add_to_cart()
   {
    $item = array(
        'id' => '42',
        'name' => 'Octane Lean Build Fast',
        'qty' => 1,
        'price' => 347,
        'options' => array ('flavor' => 'chocolate')
       );
        
    $return = $this->cart->insert($item);
    echo "<pre>";
    print_r($this->cart->contents());
    echo "</pre>";
   }
  
  
  function show_cart()
      {
    $cartcontents = $this->cart->contents();
    echo "<pre>";
    print_r($cartcontents);
    echo "</pre>";
  
   }




}


Now every time I load /dazcart/add_to_cart it confirms that the item was successfully stored into the cart.

But if I load /dazcart/show_cart I just get an empty array, telling me that he data isn't being passed across the session.

I know this is newbie s**t but I've spent a while on it now and I just want to figure this out move on so I can learn more.

Do I have to manually set up the table for the CI sessions to work? I did try that but it just threw out errors. Or will it create the table and fields if they don't exist.

Much appreciated help
#2

[eluser]alvaroeesti[/eluser]
how exactly did you try to build the sessions table and what exact errors did it spit?
#3

[eluser]dazamate[/eluser]
Update

I fixed the database problem by creating the sessions table.

here is a screen shot of the structure http://clip2net.com/clip/m84659/13452967...p-17kb.png

I can see it working with data being written in there but still the shopping cart class does not seem to be passing data from page to page.

Also I don't get any errors on the showcart page, it just prints an empty array Sad
#4

[eluser]alvaroeesti[/eluser]

And how does your model look like?

The full code I mean
#5

[eluser]dazamate[/eluser]
This is all I've done so far, no model implemented yet.

That's what I mean, I have manually inserted an item into the cart and it's not being carried across pages. Such a basic bit of code, but because I am so fresh to CI I am not sure what to do to debug this.

#6

[eluser]alvaroeesti[/eluser]
what then, what is "cart" supposed to be if it is not a Model ?

How can you insert or select any item if you don't have a model setup with its sql queries ?
#7

[eluser]dazamate[/eluser]
I understand this is a useless program, as I said I am following one of the tutorials on how to build a shopping cart. I got this far and my code isn't doing what it is suppose to, I don't want to continue until I can fix this simple code because if this doesn't work then nothing will.
#8

[eluser]alvaroeesti[/eluser]
but "contents" is supposed to be a function within the "cart" model, if you haven't created the function nor the model, how do you expect to have anything stored and returned?
#9

[eluser]dazamate[/eluser]
That doesn't sound right, I thought cart->contents() was a class method inside the cart class?

Besides, the video shows the guy building from scratch and I've got the exact same code as he does, and his item was transferred between pages without touching a model. I've done exactly what he has done...

http://net.tutsplus.com/tutorials/php/co...ping-cart/
#10

[eluser]alvaroeesti[/eluser]
without my watching the video yet

what do you think this can be ?

Code:
$return = $this->cart->insert($item);

$cartcontents = $this->cart->contents();




Theme © iAndrew 2016 - Forum software by © MyBB