Welcome Guest, Not a member yet? Register   Sign In
Loading a Model
#1

[eluser]Dizza[/eluser]
Hey everyone,

Im having a problem with loading a model function in my controller function.

First i load the model in my controller with:

Code:
$this->load->model('cmsm');

In my Controller i have:

Code:
function getmsg(){
                $this->cmsm->getmsg();
        $this->load->view('admin/updateform.php', $data);
    }

Then in my model (cmsm.php) i have:

Code:
function getmsg(){
        $this->db->where('id', $this->uri->segment(3));
        $data['query'] = $this->db->get('content');
        
    }

When i just copy and paste the model function within my controller function i dont get an error. If i do it seperate like this i get this error:

Code:
Severity: Notice

Message: Undefined variable: data

Filename: controllers/cms.php

Line Number: 22

Severity: Notice

Message: Undefined variable: query

Filename: admin/updateform.php

Line Number: 16

Code:
Fatal error: Call to a member function row_array() on a non-object in system/application/views/admin/updateform.php on line 16

What am i doing wrong here?

Thanks!
#2

[eluser]cahva[/eluser]
$data inside a model is only seen to that method. Dont try to set $data in your model, instead return the result:
Code:
function getmsg()
{
    $this->db->where('id', $this->uri->segment(3));
    return $this->db->get('content');
}

And in controller:
Code:
$data['query'] = $this->cmsm->getmsg();
$this->load->view('admin/updateform.php', $data);
#3

[eluser]mi6crazyheart[/eluser]
Update your controller function to this.....
Code:
function getmsg()
{
$data['query'] = $this->cmsm->getmsg();
$this->load->view('admin/updateform.php', $data);
}

also update u'r model function to this.....
Code:
function getmsg(){
        $this->db->where('id', $this->uri->segment(3));
        $query = $this->db->get('content');
        return $query->result();
    }

Try this. Hope it'll work.......
#4

[eluser]Dizza[/eluser]
Thanks! Both solutions work Big Grin

Thank you very much!




Theme © iAndrew 2016 - Forum software by © MyBB