You are on page 1of 8

MVC Applications

Creating a Web Application with


Model-View-Controller
Part 4
• Add directory called “model”
• Create a read function in
test_functions.php
• Call the function from index.php
• Pass the read data to the display.php
• Extract the read data as HTML
Add directory called “model”
model view
test_functions.php header.php

index.php display.php

enter.php

footer.php

controller
Add directory called “model”
Create a read function in
test_functions.php
<?php
function read_comments(){
return array( array( id => 1,
date => '12/15/2014',
name => 'Brendan',
message => 'Did we do anything important in class?' ),
array( id => 2,
date => '11/09/2014',
name => 'Joe',
message => 'Hey you know…' ),
array( id => 3,
date => '1/23/2015',
name => 'Taquila',
message => 'Grey makes my eyes cry' )
);
}
?>
Call the function from index.php

and pass data to display.php

model view
<?php
test_functions.php header.php
require('./model/test_functions.php');

// more code here


display.php
if($action == "display"){
$comments = read_comments();
include ("./view/display.php"); enter.php
}

// more code here


footer.php
?>
Extract the data as HTML 

in display.php
<?php include('header.php'); ?>
<div id="container">

<p align="right"><a href="./index.php?action=enter">Add</a></


p>

<h2>Comments</h2>

<?php foreach ($comments as $comment) : ?>


<p>&#8220<?php echo $comment['message']; ?>&#8221</
p><p>
by <strong><?php echo $comment['name']; ?></strong>
on <em><?php echo $comment['date']; ?></em>
<hr></p>
<?php endforeach; ?>

</div>
<?php include('footer.php'); ?>
Now you should have…

You might also like