Welcome Guest, Not a member yet? Register   Sign In
Undefined property: CI_Loader::$cart
#1

[eluser]qq71[/eluser]
This should be an easy one, but I can't figure it out.

I have this in autoload

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

In my controller:

Code:
$data = array(
                       'id'      => '1234',
                       'qty'     => 1,
                       'price'   => 10.00,
                       'name'    => 'product name');
                
$this->cart->insert($data);


In my view:

Code:
echo $this->cart->format_number($this->cart->total());

I get the following error

Code:
Message:  Undefined property: CI_Loader::$cart

What am I missing?
#2

[eluser]WanWizard[/eluser]
CI objects are not assigned to views when they are loaded.

You should keep the code in a view limited to what's needed for the layout of the page. All other processing should take place in your controller (or models).
#3

[eluser]vitoco[/eluser]
Two options :
1.- pass the cart object to the view as a variable :
Code:
// IN THE CONTROLLER
$this->load->view('path/to/view' , array( 'cart' => &$this->cart ));
// IN THE VIEW
echo $cart->format_number($this->cart->total());
2.- pass the value of $this->cart->format_number($this->cart->total()); to the view directly
Code:
// IN THE CONTROLLER
$this->load->view('path/to/view' , array( 'total_formatted' => $this->cart->format_number($this->cart->total()) ));
// IN THE VIEW
echo $total_formatted;

Saludos
#4

[eluser]qq71[/eluser]
Thank you!

The manual is a bit misleading.

Code:
http://ellislab.com/codeigniter/user-guide/libraries/cart.html

The section "Displaying the Cart" explains using statements inside the view
Code:
$this->cart->contents()




Theme © iAndrew 2016 - Forum software by © MyBB