Destramic Posted September 14, 2011 Share Posted September 14, 2011 heys guys just a simple question...bascially in my model i have a method that get rows from a table and those rows are presented in my template file in a loop. now what i wanna do is have a update in the loop its self to change column values of that particular row...how can i do this really? it is ok to somehow call the model in the template file? $this->news->update_status($val, $val2); i be happy if someone could help me on this question...thank you Quote Link to comment https://forums.phpfreaks.com/topic/247159-calling-a-model-method-in-view-template/ Share on other sites More sharing options...
trq Posted September 15, 2011 Share Posted September 15, 2011 That type of logic belongs in your controller, not your view. Quote Link to comment https://forums.phpfreaks.com/topic/247159-calling-a-model-method-in-view-template/#findComment-1269456 Share on other sites More sharing options...
Destramic Posted September 15, 2011 Author Share Posted September 15, 2011 thanks for your post thorpe...can i load the controller method in the template to have a update in a loop? Quote Link to comment https://forums.phpfreaks.com/topic/247159-calling-a-model-method-in-view-template/#findComment-1269678 Share on other sites More sharing options...
Destramic Posted September 15, 2011 Author Share Posted September 15, 2011 maybe this will help you to understand what im trying to do template where i want to update in loop <table> <tr> <th id="rank">Rank</th> <th id="team">Team</th> <th id="points">PTS</th> <th id="matches_played">MP</th> <th id="wins">W</th> <th id="losses">L</th> <th id="draws">D</th> <th id="streak">W Streak</th> </tr> <?php foreach ($this->rows as $rows):?> // update row in loop <tr> <td><?= $rows['rank'] ?>.</td> <td><?= $rows['team_name'] ?></td> <td><?= $rows['points'] ?></td> <td><?= $rows['matches_played'] ?></td> <td><?= $rows['wins'] ?></td> <td><?= $rows['losses'] ?></td> <td><?= $rows['draws'] ?></td> <td><?= $rows['win_streak'] ?></td> </tr> <?php endforeach; ?> </table> or should i just create a loop in my controller of $this->rows and do the updates there?....if you could tell me what the best practice would be plesase Quote Link to comment https://forums.phpfreaks.com/topic/247159-calling-a-model-method-in-view-template/#findComment-1269725 Share on other sites More sharing options...
trq Posted September 16, 2011 Share Posted September 16, 2011 No business logic should take place in your views, they are for displaying data only. Quote Link to comment https://forums.phpfreaks.com/topic/247159-calling-a-model-method-in-view-template/#findComment-1269772 Share on other sites More sharing options...
Destramic Posted September 16, 2011 Author Share Posted September 16, 2011 ok thanks note taken...so should i just do the loop update in the controller would that be correct? Quote Link to comment https://forums.phpfreaks.com/topic/247159-calling-a-model-method-in-view-template/#findComment-1269826 Share on other sites More sharing options...
trq Posted September 16, 2011 Share Posted September 16, 2011 Yes, that would be correct. Of course, there would be no markup in your controller though. Quote Link to comment https://forums.phpfreaks.com/topic/247159-calling-a-model-method-in-view-template/#findComment-1270101 Share on other sites More sharing options...
Destramic Posted September 16, 2011 Author Share Posted September 16, 2011 sorry no markup in the cotroller? Quote Link to comment https://forums.phpfreaks.com/topic/247159-calling-a-model-method-in-view-template/#findComment-1270131 Share on other sites More sharing options...
KevinM1 Posted September 16, 2011 Share Posted September 16, 2011 sorry no markup in the cotroller? Correct. Only views should contain HTML. Here's what MVC usually breaks down into: Model: This is your site's internal logic. Not the logic that decides which URL was requested, or what view to render, but the logic of doing things with data. It could be as simple as run-of-the-mill database CRUD operations, to complex business/financial activity. Controller: This is the part that handles HTTP requests (GET and POST), accesses the Model layer for data processing, and decides which View to render while sending the Model data to that View. View: This is what the user sees. They're HTML templates with very, very little PHP to help render the Model data (if/else conditionals and loops only). With most MVC systems, Controllers are accessed according to the URL supplied to the system. The system has a Front Controller (look it up) and a route table. The Front Controller parses the incoming URL and attempts to find a match in the route table. When a match is found, the correct Controller object is retrieved, and the desired method is invoked, with whatever request data sent in as arguments to that method. Example: example.com/news/story/1223 The news story method of the news controller is being accessed, with 1223 as the argument being sent in. In other words: class NewsController extends Controller { public function story($id) { // access the Model to obtain the news story data associated with $id // render the correct View with that news story data } } Before building your own MVC framework, you should really take a look at how they're usually done. There are some conventions to follow that will make your life easier. Quote Link to comment https://forums.phpfreaks.com/topic/247159-calling-a-model-method-in-view-template/#findComment-1270139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.