Welcome Guest, Not a member yet? Register   Sign In
Trouble passing results from the model to the controller
#1

[eluser]seavers[/eluser]
Hi,

I'm really new to Ci and OOP so please bear with me on this.

I am testing the CI framework, and thought I'd build a little CMS to familiarise myself with CI. I have a function in my 'admin' controller:

Code:
function logUser() {
    
    
        $this->load->model('Usermodel');
    
        $username = $this->input->get_post('username', TRUE);
        
        $password = $this->input->get_post('password', TRUE);
    
        $query = $this->db->query('SELECT * FROM user WHERE username = \''.$username.'\'');
        
        $result = $query->row();
        
        if ($query->num_rows() > 0) {
            
            echo $result->password;            
        }
        
        else {        
        
            echo "Who are ya!";    
                
        }
    
    }

Which works ok, but I would like to separate the data stuff into a nice model file, called Usermodel. So, I created a model, and amended the function above to:

Code:
function logUser() {
    
    
        $this->load->model('Usermodel');
    
        $username = $this->input->get_post('username', TRUE);
        
        $password = $this->input->get_post('password', TRUE);
    
        $result = $this->Usermodel->checkUser($username, $password);
        
        if ($query->num_rows() > 0) {    
            
            echo $result->password;            
        }
        
        else {        
        
            echo "Who are ya!";    
                
        }
    
    }


My corresponding function in my model file looks like this:

Code:
function checkUser($username, $password) {

        $query = $this->db->query('SELECT * FROM user WHERE username = \''.$username.'\'');
        
        $result = $query->row();
        
    }

But unfortunately, I get an error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: controllers/admin.php

Line Number: 31

Fatal error: Call to a member function num_rows() on a non-object in C:\xampp\htdocs\CodeIgniter\system\application\controllers\admin.php on line 31

All I have done is to move the code from the controller, to the model. I guess I'm not passing an object back to the controller, how do I do this?

Thanks,

James


Messages In This Thread
Trouble passing results from the model to the controller - by El Forum - 10-21-2008, 04:21 PM



Theme © iAndrew 2016 - Forum software by © MyBB