Beep
Beep

Reputation: 2823

Using active records to display data in codeigniter

so I believe I have it right (im new to active records). the problem I am having I keep getting error

Message: Call to a member function getbank() on a non-object

My code is simple

Controller

class Profile extends CI_Controller
{
    function index()
    {
        $data = array();

        if($query = $this->profile_model->getbank())
        {
            $data['records'] = $query;
        }

        $this->load->view('profile_view', $data);
    }

}

model

   class Profile_model extends CI_Model {

    function getbank()
{
    $query = $this->db->get('bank');
    return $query->results();
}

Its so simple, I cant see a error, thank you.

Upvotes: 1

Views: 124

Answers (2)

Abdulla Nilam
Abdulla Nilam

Reputation: 38642

Try this

Controller

class Profile extends CI_Controller
{
    function index()
    {
        $data = array();
        $this->load->model('profile_model'); # Added
        $query = $this->profile_model->getbank(); # Changed

        if(!empty($query)) # Changed
        {
            $data['records'] = $query;
        }

        $this->load->view('profile_view', $data);
    }
}

Model

class Profile_model extends CI_Model {

    public function getbank()  # Changed
    {
        $query = $this->db->get('bank');
        return $query->result();  # Changed
    }
}

Update

Simple answer, thanks to all those who tried helping. I had the model file name with a small letter not a capital.

Eg profile_model.php

should be Profile_model.php

However, this solution is elegant and simple, so it improved my code so il accept this one.

Upvotes: 3

Amous
Amous

Reputation: 514

It may be because you didn't load your model? Try changing your controller to:

class Profile extends CI_Controller
{
    function index()
    {
        $data = array();

        $this->load->model('Profile_model');

        if($query = $this->Profile_model->getbank())
        {
            $data['records'] = $query;
        }

        $this->load->view('profile_view', $data);
    }

}

Upvotes: 2

Related Questions