开发者

Accessing Model from View

I have a foreach loop which generates links with different IDs. I have an ajax function to process this ID and the username (from session) to add a record to the table. (It's like enrolling for some events).

I want to make an additional statement which will check whether the user has already enrolled for the event with some ID and if so, the link will be deactivated or change color. I've tried creating another variable (which is passed to the view)

$data['myvariable'] = $this->mymodel->myfunction();

This function in model checks all the records from the database where user's username appears and insert to arra开发者_StackOverflowy all event IDs. I've tried adding an additional if statement before links in foreach loop which checked whether the ID from link is in array but there appeared some problems with the controller. (i think that I couldnt assign the array to the variable $data['myvariable'] ).

I know that accessing to model from view is not "proper"... Anyone know how to solve this problem?


Do you have a field in the DB to store whether or not the user has enrolled? Is so, run a select query on that table checking for that value then use a conditional statement to effect the link. Kinda like this

$enrolled = $this->your_model->your_method($param);
if($enrolled){

process links here;

}


I'd say it depends on the MVC your using also. You may have to load the model in the view to use it from within the view, not ideally the right approach as you could load it via your controller and set it as a variable to be passed into the view if the variable to be passed contains multiple outputs such as a query from a DB or something you can pass it to the view as an array object and then in the view do a while, for, foreach, whatever on that array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜