browser lang:en
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);
}
}
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');
<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>
Selective Tweets is an application that allows you to control what tweets are added to your Facebook…
in:Social Networking (0 comments)In a FBML Facebook App, your quick jump menu will require a little tweak to work in FBJS…
in:Scripts and tutorials (0 comments)Every single day, someone, somewhere is discussing something important to your business; your brand, your executives, your…
in:Scripts and tutorials (0 comments)Although some IT managers are coming around to the idea of supporting iPhones, most aren’t convinced…
in:Free Software (0 comments)When you want to run your own mail server, and it does not matter what version and…
in:Apache - .htaccess (0 comments)Have you ever had to develop something yourself only to find out that there had already been…
in:JQuery (0 comments)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)So. Google just recently announced Google Buzz. I’m not sure about you, but I…
in:Blog (0 comments)
