Introduction to MVC using PHP

To better demonstrate how MVC works, let’s use the example of a simple data retrival both using MVC and not using it.

One of my amigo asked me to demonstrate how MVC pattern actually works. We actually know what’s Model,View and Controller are all about.Anyhow let me explain them in detail before proceeding with code area. The Traditional way of connecting to the Database and fetching the results.

<?php
$link = mysql_connect(‘localhost’, ‘mysql_user’, ‘mysql_password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
$query_results = mysql_query(’select * from searchNames order by firstname desc’);
?>
<html>
<body>
<h1>List of Datas</h1>

<?php while ($row = mysql_fetch_object($query_results)) { ?>
<h2><?php echo $row->firstname ?></h2>
<h2><?php echo $row->lastname ?></h2>
<?php } ?>
</body>
</html>

The Traditional way connects to the database with a connection string, and then the $result variable holds a mysql_query. Then We have a common way of displaying the results made through the query.Now lets jump into MVC for the above code.

Responsibilities of the model

The model stores data in properties and provides application-specific methods that set and retrieve that data.The model must also provide a way for views to register and unregister themselves, and it must manage a list of registered views. Whenever the model determines that its state has changed meaningfully, it must notify all registered views.

The below code shows the Model.

<?php
function methodName()
{
$link = mysql_connect(‘localhost’, ‘mysql_user’, ‘mysql_password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
$query_results= mysql_query(’select * from searchNames order by firstname desc’);

$data = array();
while ($row = mysql_fetch_objects($query_results)) {
$data[] = $row;
}
return $data;
}
?>

Responsibilities of the Controller

The controller listens for notifications from the view based on user input and translates that input into changes in the model.

Here is what Controller tells us

<?php
$data= methodName();
display_template(‘data.tpl’);
?>

Responsibilities of the view

The view must create the user interface and keep it up-to-date. The view listens for state changes in the model; when the model changes, the view updates the interface to reflect the change.

Now the Climax the view part

<html>
<body>
<h1>List of Datas</h1>

<?php foreach ($data as $row) { ?>
<h2><?php echo $row->firstname ?></h2>
<h2><?php echo $row->lastname?></h2>
<?php } ?>
</body>
</html>

How actually it works?
When we call the url [ I use codeIgnitor framework for development ]. http://www.domain-name.com/index.php/theData/ [ Controller ], it interacts with view and the model to display the data. The Controller acts as a medium between view and model.

Popularity: 100% [?]

About the Author

Vinothbabu has written 23 stories on this site.

In short, my name is Vinoth babu { perra kettala summa authrithala } a UI expert from India, Chennai.

14 Comments on “Introduction to MVC using PHP”

  • John keppler wrote on 26 December, 2008, 8:45

    One of the best article which describes about the MVC pattern. Kudos

  • tech news wrote on 8 January, 2009, 6:55

    Such a good information no need to use any framework.

  • Vinothbabu wrote on 8 January, 2009, 10:22

    The reason why people opt for various framework is the featurs and ability to use their libraries and methods which come in handy.

    I personally like codeignitor which is light, but in most big projects i go for my own designed pattern rather than using some other.

  • zahid wrote on 29 January, 2009, 6:17

    such a great topic

    http://www.zahipedia.com

  • andrea genesis wrote on 18 February, 2009, 8:13

    nice article,..im still confuse coz it doesnt work on my localhost…

  • Vinothbabu wrote on 24 February, 2009, 13:33

    What was the error you got while trying with this piece.

  • Aibe wrote on 16 April, 2009, 17:15

    It feels like something is missing here… what does display_template(’data.tpl’) do – what framework is this using. Sorry – I’m not getting this.

  • Vinothbabu wrote on 17 April, 2009, 12:54

    Aibe… just tell me what error you are getting when u use the display_template. I will explain you further how it works… it will help you also get that.

  • Alessandro (Programmatore PHP) wrote on 4 June, 2009, 10:20

    Perfect example Vinothbabu!
    I think is important to use a framework to use MVC in a best way, a good framework is http://framework.zend.com , is open source, freem and developed by Zend, The PHP company, and is a very good tool for medium and big projects.

  • Prema wrote on 18 June, 2009, 6:33

    Hi
    I tried but the below error occurs “Fatal error: Call to undefined function display_template() ”

    I save the following in index.php

    List of Datas

    firstname ?>
    lastname?>

    And this is under sample.php

    Please clarify.
    I dont know from where to call the method.
    Thanks in advance.

  • sarwar wrote on 12 July, 2009, 3:24

    Please clarify.
    I dont know from where to call the method.
    Thanks in advance.

  • Hudson FAS wrote on 22 January, 2010, 12:21

    function display_template(tpl_file) {
    import ($tpl_file);
    }

  • Hudson FAS wrote on 22 January, 2010, 13:48

    function display_template($tpl_file) {
    require_once($tpl_file);
    }

  • Keyur Patel wrote on 17 February, 2010, 5:32

    very simple yet very effective and to-point introduction, thanks

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 HTTPguru. All rights reserved.

Our Partners:
WebHosting Jobs| UI expert | Packet Sniffer  | Mobile Home Refinancing  | Laptop LCD screens | CSS Creator| Exchange Hosting| Compare cable companies| Usa online casino | vps | Coupon Codes