Main Menu




browser lang:en

Users Area

Client Login

today cache size is:0

 

 


Comments

fg
16/04/2011 by fdg
Hi all
13/02/2011 by
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

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
Upbooking - free booking engine

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.


Comments

Insert your comment

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

Users

Videos


Scripts & Tutorials

50 necessaries php tools

25/02/2011 

PHP is one of the most widely used open-source server-side scripting languages that exist today. With over…

in:Scripts and tutorials (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)

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)

PHP - calculating distance between two points

20/10/2009 

Because of the near-spherical shape of the Earth, calculating an accurate distance between two points requires the use…

in:Scripts and tutorials (1 comments)

PHP - Download file with speed limit

20/10/2009 

With this script we can limit the download speed   // local file that should be send to the client $local_file…

in:Scripts and tutorials (0 comments)

PHP - Save remote images on our server using CURL

12/10/2009 

Some hosts disabled the ini setting allow_url_fopen. This also means that the ability to easily grab images…

in:Scripts and tutorials (0 comments)

PHP - verify file existence in a local server

11/06/2009 

The act to verify if a file exists, is one of more important tasks related to files operations,…

in:Scripts and tutorials (0 comments)

Read more »


Tag Clouds


necessaries toolsFBJS Quick Jump Menu FBML Facebook Platform AppHow manage your online reputation free tools forcommunity managersPHP Singleton PatternPHP calculating distance between points Download file speed limitPHP Save remote images


Add to Technorati Favorites