MVC Question

If you are writing display code, for example to output data from a news table, which section would the PHP output code go in?

Thanks E.

Well the outputting is controlled by the controller, but the overall output code would be in the view. For example if you wanted to display a list from an array, you could have the array be sent to the view from the controller, and in the view you could do your loop through the data.

Assuming you’re using a framework? I know CodeIgniter is a lot less restricting as far as where model/controller code goes, pretty sure you can go without even using the models.

I recommend reading up on MVC before delving into it:

That is not really MVC. In MVC the controller does nothing with the output, it’s handled by the view entirely. The view also has direct access to the model and does not get passed data from the controller. Here’s a better example:

In MVC, The controller is NOT a Mediator between the view and the model. The controller does not sit in between the model and the view. Both the controller and the view have equal opportunity to access the model. The controller does not copy data values from the model to the view, although it may place values in the model and tell the view that the model has changed. see Presentation Abstraction Control where the control layer acts as a mediator between Presentation and Abstraction.

Thank you for these resources. I’ll read them.

But say the following code is used to display data. (abbreviated hypothetical example):

$sql='SELECT * FROM staff';
$result=mysql_query($sql) or die(mysql_error());
echo "$Name, $Title $Bio";

Would this code:

  1. go in the view
  2. be a function in the controller accessed in the view as <?php=get_staff() ?>
  3. or go in a function in the model

Thank you E

That code is both model and view code and would need to be split:

model code:

function findStaff() {
$sql='SELECT * FROM staff';
$result=mysql_query($sql) or die(mysql_error());
$results = array();
while ($row = mysql_fetch_array($result)){
$results[] = $row;
return $results;

View code:

foreach (findStaff() as $staff) {
echo $staff['Name'] . ',' .  $staff['Title'] . ',' . $staff['Bio'];

Keep in mind that MVC is far easier to implement with OOP. Your database connection would be encapsulated as a member variable in the model.

edit: There is no controller code here because the controller is concerned with user interaction… and the user has no affect on this example.

Interesting, thank you. -Michael