简体   繁体   中英

Getting error in loading model in controller in codeigniter Message: Undefined property: Cart::$load

I am a beginner in codeigniter and currently I am working on a shopping cart, taking help from http://net.tutsplus.com/tutorials/php/how-to-build-a-shopping-cart-using-codeigniter-and-jquery/ tutorial. I am using codeigniter 2.1.3.

I am getting an error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Cart::$load

Filename: controllers/cart.php
Line Number: 7

Fatal error: Call to a member function model() on a non-object in D:\\xampp\\htdocs\\ci\\application\\controllers\\cart.php on line 7

Can someone please tell me why it is not working?

The name of my controller is cart.php

<?php
class Cart extends CI_Controller {

    public function Cart()
    {
        //parent::CI_Controller(); // We define the the Controller class is the parent. 
        $this->load->model("cart_model"); // Load our cart model for our entire class
    }

    public function index()
    {
        $data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
        print_r($data['products']);
        //$data['content'] = 'cart/products'; // Select view to display
        //$this->load->view('index', $data); // Display the page
    }
}
?>

and my model is cart_model.php

<?php
class Cart_model extends CI_Model{
    //public function _construct(){
        //parent::_construct();
    //}

    public function retive_products(){
        $query = $this->db->get("products");
        return $query->result_array();
    }
}
/* End of file cart_model.php */  
/* Location: ./application/models/cart_model.php */
?>

Try to review your code. You calling the retrieve_products function on model but in model you have retive_products function .

Controller

public function index() {
      $data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products
      print_r($data['products']);

      //$data['content'] = 'cart/products'; // Select view to display
      //$this->load->view('index', $data); // Display the page
}

Model

public function retive_products() {
      $query = $this->db->get("products");
      return $query->result_array();
}

Codeigniter 2.1.3 is intended to support PHP 5.2.4 and newer.

Change the class constructor:

<?php
  class Cart extends CI_Controller {
    public function __construct()
      {
         parent::__construct();
      }

instead of

public function cart()
    {
        parent::CI_Controller();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM