I’m writing a simple app. I need to block user from a page if their credit is < 0.
I have a table “User_profiles” with a “credit” row.
How can I set up a model in conjunction with the controller to send the user to another page if the value of “credit” is 0?
This should be straightforward, but I’m new at the select->where stuff…
It has to be the row of the current user too–I don’t know how to traverse arrays very well yet.
Thanks!
Well, the easiest solution would be to just load a different view…
As for the model, it would look like this:
Then in the controller:
That’s untested, but it should at least help you get the idea.
When you request the page
/users/credit/1, CI will call theUsers::credit(1)action.It then loads
UserModelas$this->usersYou call
$this->users->getUserCredit(1), which translates toUserModel::getUserCredit(1), to store as$creditThe model loads the database.
You tell the db to
select('credit')(select thecreditcolumn),where('id',1)(where theid = 1), thenget('User_profiles')(get matching rows from theUser_profilestable). That returns a query, which you store as$queryfor readability.getUserCreditreturns the credit property of the single-row result of the queryIf
$credit == 0, you load the viewviews/users/no_credit.phpOtherwise, you load the view
views/users/credit.php(it’s conventional to name the views after the actions they represent and put them in a folder corresponding to the controller)