Main Menu




browser lang:en

Users Area

Client Login

today cache size is:82392

 

Holyguard rss
rss 2.0 for all sections

 

Portfolio

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




Comments

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
really good
02/11/2008 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
3 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

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)

Display an image from a MySQL database in a web page via PHP

14/02/2010 

There's lots of clever scripts around to tell you how to get images in and out of…

in:PHP scripts (0 comments)

MySQL - Find Locations Nearest Known Coordinates

03/02/2010 

Spherical Law of Cosines Suppose that we want to find the five nearest places to (47.470779, -87.890699) using Spherical…

in:MySql (0 comments)

The 7 free best CSS editors

01/02/2010 

I have always used Dreamweaver, and love it. But, I have been thinking, what are the FREE CSS…

in:Free Software (0 comments)

Upgrade to Alsa 1.0.20 on Ubuntu Jaunty 9.04

09/01/2010 

Advanced Linux Sound Architecture (known by the acronym ALSA) is a Linux kernel component intended to replace the…

in:Blog (0 comments)

PHP - The Singleton Pattern

26/11/2009 

The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…

in:Scripts and tutorials (0 comments)

Read more »


Tag Clouds


Complete guide Jquery DevelopersHow insert HTML code posts12 undocumented tricks Google BuzzDisplay image from MySQL database page PHPMySQL Find Locations Nearest Known CoordinatesThe free best editorsUpgrade Alsa 1020 Ubuntu Jaunty


Add to Technorati Favorites