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
#2

[eluser]Pascal Kriete[/eluser]
Yep, $query is not defined in the controller. It's only available in the scope of the function that you defined it in.

The obvious solutions would be:
1 - move the if statement to the model.
2 - return the query object instead of the results.

I usually use a variation of 1. I check for results in the model, but keep the rest of the logic in the controller by returning false to indicate no results.
Code:
return ($query->num_rows() > 0) ? $query->row() : FALSE;

Then in the controller you run a boolean check:
Code:
if ( ! $result)
{
    die("whatever");
}
//...

Welcome to CodeIgniter.
#3

[eluser]lmv4321[/eluser]
In the logview function, try changing this:
[quote author="seavers" date="1224645661"]
Code:
if ($query->num_rows() > 0) {

[/quote]

to this:
Code:
if ($result->num_rows*( > 0) {

And also change checkUser from this:
[quote author="seavers" date="1224645661"]
Code:
$result = $query->row();

to this:
Code:
return $result;


I did not test this, so I cannot verify that it will work, but it may give you an idea.
#4

[eluser]seavers[/eluser]
Whoa, great response, thanks guys.

Lab Technicians answer works, and just to double check I understood the code, I converted it back to longhand Wink

Code:
if ($query->num_rows > 0) {
        
            return TRUE;
            
        }
        
        else {
        
            return FALSE;
            
        }

I shall do my checks in the model, makes sense to just return a boolean. Cheers!
#5

[eluser]seavers[/eluser]
Sorry :red: my code above is wrong. LT's statement works correctly:

Code:
return ($query->num_rows() > 0) ? $query->row() : FALSE;

But just so that I can understand it, what is the longhand version?
#6

[eluser]Pascal Kriete[/eluser]
Hehe, look a little higher for the usernames Wink .

It's called a ternary operation. Essentially it takes this basic form:
Code:
$value = condition ? if_true : if_false;

Which you could write as:
Code:
if (condition)
    $value = if_true;
else
    $value = if_false;

If you wanted to return $value, you would not assign it to a variable first - and the same concept applies here. So the code I posted translates to this:
Code:
if ($query->num_rows() > 0)
{
    return $query->row();
}
else
{
    return FALSE;
}

Hope that helps.
#7

[eluser]Randy Casburn[/eluser]
I just call him the BIG EYE BALL...hehehehe <<that's just crrrreepy inparo!>>

R
#8

[eluser]seavers[/eluser]
Thanks inparo, that's exactly what I needed.
#9

[eluser]Pascal Kriete[/eluser]
Randy, considering that that is my eyeball, you wouldn't want to see the rest of me Smile .
I tried to photoshop out the skin, but it was beyond my skill.
#10

[eluser]Randy Casburn[/eluser]
That just ruined my day man...the site picture I mean...besides you are always looking at me, and staring, and...and it makes me nervous. I'm going to see my therapist now. %-P

R




Theme © iAndrew 2016 - Forum software by © MyBB