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>
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)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)The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…
in:Scripts and tutorials (0 comments)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)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)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)The act to verify if a file exists, is one of more important tasks related to files operations,…
in:Scripts and tutorials (0 comments)
