![]() |
Trouble passing results from the model to the controller - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Trouble passing results from the model to the controller (/showthread.php?tid=12503) |
Trouble passing results from the model to the controller - El Forum - 10-21-2008 [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() { 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() { My corresponding function in my model file looks like this: Code: function checkUser($username, $password) { 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 Trouble passing results from the model to the controller - El Forum - 10-21-2008 [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) Welcome to CodeIgniter. Trouble passing results from the model to the controller - El Forum - 10-21-2008 [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. Trouble passing results from the model to the controller - El Forum - 10-21-2008 [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 ![]() Code: if ($query->num_rows > 0) { I shall do my checks in the model, makes sense to just return a boolean. Cheers! Trouble passing results from the model to the controller - El Forum - 10-21-2008 [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? Trouble passing results from the model to the controller - El Forum - 10-21-2008 [eluser]Pascal Kriete[/eluser] Hehe, look a little higher for the usernames ![]() 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) 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) Hope that helps. Trouble passing results from the model to the controller - El Forum - 10-21-2008 [eluser]Randy Casburn[/eluser] I just call him the BIG EYE BALL...hehehehe <<that's just crrrreepy inparo!>> R Trouble passing results from the model to the controller - El Forum - 10-22-2008 [eluser]seavers[/eluser] Thanks inparo, that's exactly what I needed. Trouble passing results from the model to the controller - El Forum - 10-22-2008 [eluser]Pascal Kriete[/eluser] Randy, considering that that is my eyeball, you wouldn't want to see the rest of me ![]() I tried to photoshop out the skin, but it was beyond my skill. Trouble passing results from the model to the controller - El Forum - 10-22-2008 [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 |