Jump to content

calling a model method in view template


Destramic

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.