Main Menu




browser lang:en

Users Area

Client Login

today cache size is:222615

 

Holyguard rss
rss 2.0 for all sections

 

Portfolio

Some of my projects:
CTIBA
MA-NO Web Agency
BluPool
L'Pratone
Travel in Hotel
Yacht Elements
AbruzzoWeb
Il Mastino
CSI Teramo
Innovazione S.p.a.




Comments

John
24/07/2010 by John
better
16/04/2010 by bob
attached files not working !
05/11/2009 by Adnan
Tuvok
14/02/2009 by
Nice script
07/02/2009 by desaj
Other solutions
07/02/2009 by Mike
well
06/02/2009 by holy
Theif
06/02/2009 by

Read More »


RAPIDQ

Rapid-Q:The lost files
My old library where i have collected all the rapidq scripts and italian help


Today my pagerank is: Free Page Rank Tool

PHP and MVC - An example of Model View Controller with PHP

Model view controller architecture has been a staple of desktop application development for a hell of a long time, with the increasing complexity of these newfangled web applications and websites in general the encapsulation and flexibility of mvc design makes darn good sense (buzzwords bolded for the executives out there). But what happens when a good design idea meets a really really evil language...

Well ok, php isn't evil per se, it's just misunderstood. Grossly, horrifyling misunderstood on a level that only economists and libertarians can appreciate (and maybe perl programmers but they enjoy that kind of thing). It's simple, php allows people to write really bad code and still accomplish whatever it is they want to do and fast. However, it is possible to write readable, maintainable code in php and using mvc will help with that. So without further ado here's my interpretation of mvc in php.

The model is the interface to the database. All database specific code goes in here. If you are using flat files, tables, and/or smoke signals do all the loading and saving in the models. Here's the model for the example.

File model: animal.mdl


class Animal
{
public static function getAnimal($animal_id)
{
  $query = "SELECT * FROM animals WHERE animal_id='$animal_id'";
  $result = mysql_query($query);
  $row = mysql_fetch_object($result);

  $animal = array();
  $animal['animal_id'] = $row->animal_id;
  $animal['color'] = $row->color;

  return animal;
}

public static function updateAnimal($animal_id, $animal_color)
{
  $query = "UPDATE animals SET color='$animal_color' WHERE animal_id='$animal_id'";
  msyql_query($query);
}
}
These functions would be called to load a specific animal and update a animal's color. I'm trying to leave out extraneous code to make the principles more clear so don't jump all over me for a lack of error checking and whatnot. Anyway, you've got a animal in the database you need the data. You load it up and stick it into an array. Not exactly rocket science. When you want to update a animal's color you send the animal_id and the color.

Now for the controller. The purpose of this part is to act as an intermediary, controlling logic flow and massaging data for the template and model. Your controller tells the template what parts of the page to display and how to display them. Here's the example:

File controller: Animal.php

include('Animal.mdl');

if ( isset($_REQUEST['animal_submit']) )
{
Animal::updateAnimal($_REQUEST['animal_id'], $_REQUEST['animal_color']);
}

$animal = Animal::getAnimal($_REQUEST['animal_id']);
include('Animal.tmpl');


This isn't going to make a whole lot of sense until you see the template file so here it is:

File template: Animal.tmpl

<form action="Animal.php" method="post">
<input name="animal_id" value="<?php echo $animal[animal_id]; ?>" type="hidden">
<input name="animal_color" value="<?php echo $animal[color]; ?>" type="text">
<input name="animal_submit" value="Update" type="submit">
</form>
The controller is the page you would link to from other parts of your site. In this case it would probably be called from a page with a list of animals, each being a link with a url like: Animal.php?animal_id=23

Before it includes the template, the controller calls on the model to load the data from the database.

The form calls the controller when the update button is pressed. Then the controller tells the model to update the databse. The important part here is that the template is only concerned with the interface to the user.

Splitting your code up this way will make your life so much easier it's ridiculous. With this design you can load up a controller and quickly get an idea of the logic flow of a page. You can swap out different data sources with a minimal amount of rewrites and you can pass off designing the templates to an artist with minimal php knowledge, or have them do mockups and you fill in the php. MVC is a Good Thing(tm), you should use it.

Rating:
73.0
9 votes
1 2 3 4 5

Comments

Insert your comment

Titolo
Messaggio
Nome Utente
e-mail (se vuoi ricevere le risposte a questo post anche via mail)

Videos


Contents

Selective Tweets Application for Facebook

21/07/2010 

Selective Tweets is an application that allows you to control what tweets are added to your Facebook…

in:Social Networking (0 comments)

FBJS Quick Jump Menu for a FBML Facebook Platform App

15/07/2010 

In a FBML Facebook App, your quick jump menu will require a little tweak to work in FBJS…

in:Scripts and tutorials (0 comments)

How to manage your online reputation, free tools forcommunity managers

02/07/2010 

Every single day, someone, somewhere is discussing something important to your business; your brand, your executives, your…

in:Scripts and tutorials (0 comments)

Genuitec MobiOne Enables Developers to Create Web Apps for the iPhone

28/05/2010 

Although some IT managers are coming around to the idea of supporting iPhones, most aren’t convinced…

in:Free Software (0 comments)

How to configure MX records for incoming SMTP e-mail traffic

27/03/2010 

When you want to run your own mail server, and it does not matter what version and…

in:Apache - .htaccess (0 comments)

Complete guide for Jquery Developers

27/02/2010 

Have you ever had to develop something yourself only to find out that there had already been…

in:JQuery (0 comments)

How can i insert HTML code in my posts?

14/02/2010 

The situation begins with your blog or website and you need to post some code on a particular…

in:The Holy Faq's (0 comments)

12 undocumented tricks for Google Buzz

14/02/2010 

So. Google just recently announced Google Buzz. I’m not sure about you, but I…

in:Blog (0 comments)

Read more »


Tag Clouds


Selective Tweets Application FacebookFBJS Quick Jump Menu FBML Facebook Platform AppHow manage your online reputation free tools forcommunity managersGenuitec MobiOne Enables Developers Create Apps iPhoneHow configure records incoming SMTP email


Add to Technorati Favorites