I think I finally got this MVC thing
Posted: Fri Aug 21, 2009 6:51 am
I've been reading this book about MVC with PHP and at first I really didn't understood the concept. I had a few ideas how to implement it but didn't get the entire picture.
On my recent project I did my best to try and implement it, the project is an army builder for a game called Warhammer.:
So I have a class for my mySQL connection which I use as an argument for my model class.
So the example is my way of dispaying a users army, any input and comments are highly appreciated:
UserArmy.php
The Controller decides what to do with the array provided. It extends the UserArmy class:
And then we have the view which adds html and displays the array in a nice way:
Lastly I have a frontController which is just a php page with a switch statement, headers, footers etc:
I bet there is a lot to work on to make this much neater, but at least I'm getting a hang of it.
Input? Be honest, I can take it...
On my recent project I did my best to try and implement it, the project is an army builder for a game called Warhammer.:
So I have a class for my mySQL connection which I use as an argument for my model class.
So the example is my way of dispaying a users army, any input and comments are highly appreciated:
UserArmy.php
Code: Select all
<?php
class UserArmy {
private $army_table, $units_table, $unit_type_table, $unit_options_table, $unit_stats_table, $options_table, $options_type_table, $user_army_table, $user_units_table, $user_unit_options_table;
function __construct($db, $user_id) {
$this->db = $db;
$this->user_id = $user_id;
$this->army_table = "a_army";
$this->units_table = "a_units";
$this->unit_type_table = "a_unit_type";
$this->unit_options_table = "a_unit_options";
$this->unit_stats_table = "a_unit_stats";
$this->options_table = "a_options";
$this->options_type_table = "a_options_type";
$this->user_army_table = "a_user_army";
$this->user_units_table = "a_user_units";
$this->user_unit_options_table = "a_user_unit_options";
}
//List User Armies
function UserArmies() {
if($link = $this->db->dbConnect()) {
$sql = "SELECT a.id, a.army_type, a.army_name, a.points, b.name
FROM {$this->user_army_table} AS a
JOIN {$this->army_table} AS b ON a.army_type = b.id
ORDER BY a.army_type, a.points ASC";
if($result = mysql_query($sql)) {
while ($row = mysql_fetch_assoc($result)) {
$army[] = $row;
}
} else return mysql_error();
}
//return $army[] => id, name etc
return $army;
}
etcCode: Select all
<?php
class UserArmyController extends UserArmy {
function __construct($db, $user_id) {
parent::__construct($db, $user_id);
}
function UserArmy($army_id) {
foreach(parent::UserArmy($army_id) as $a_id => $value) {
if(is_array($value)) {
foreach($value as $u_id => $unit) {
$e_cost = "";
//Unit Equip
$value[$u_id]['equip'] = parent::UserUnitOptions($army_id, $value[$u_id]['id'], $value[$u_id]['unit_id']);
//Calculate Equipment cost
if(is_array($value[$u_id]['equip'])) {
foreach($value[$u_id]['equip'] as $key => $option) {
switch($option['option_type']) {
case 3:
case 4:
case 11:
case 13:
$e_cost += $option['cost'];
break;
default:
$e_cost += $option['cost'] * $unit['unit_amount'];
}
}
}
//Unit Cost
$value[$u_id]['unit_cost'] = $unit['cost'] * $unit['unit_amount'] + $e_cost;
$total_cost += $value[$u_id]['unit_cost'];
}
}
$ua[$a_id] = $value;
$ua['army_cost'] = $total_cost;
}
return $ua;
}
}
?>Code: Select all
<?php
class UserArmyView {
function __construct(UserArmyController $army) {
$this->Controller = $army;
}
function UserArmiesView() {
echo "<h4>Army Roster</h4>\n";
echo "<div style=\"margin-left: 1em\">\n";
echo "\t<ul>\n";
foreach($this->Controller->UserArmies() as $key => $value) {
echo "\t\t<li><a href=\"{$_SERVER['SCRIPT_NAME']}?a=my_army&s=view_armies&id={$value['id']}\">{$value['name']}</a>";
if(!empty($value['army_name'])) echo " - <i>{$value['army_name']}</i>";
echo " ({$value['points']})pts</li>\n";
}
echo "\t</ul\n";
echo "</div>\n";
}
function UserArmyView($id) {
//Fetch Army Information
$ua = $this->Controller->UserArmy($id);
echo "<hr>\n";
echo "<h4>{$ua['army_type']}</h4>\n";
echo "<p><b>Name:</b> {$ua['army_name']}<br />\n";
echo "<b>Points:</b> {$ua['army_cost']}/{$ua['points']}pts<br />\n";
echo "<b>Created:</b> {$ua['date']}</p><br />\n";
//Display Units
if(is_array($ua['units'])) {
echo "<h5>Units</h5>\n";
foreach($ua['units'] as $unit) {
$e = "";
echo "<p><b>{$unit['name']}</b><br />\n";
//Display Equipment
if(is_array($unit['equip'])) {
echo "<b>Equipment:</b> ";
foreach($unit['equip'] as $equip) {
$e .= $equip['name'].", ";
}
echo rtrim($e, ", ") ."<br />\n";
}
echo "<b>Unit Cost:</b> {$unit['unit_cost']}pts</p><br />\n";
}
}
}
}
?>Code: Select all
...
<?php
switch($_GET['s']) {
//View User Armies
case "view_armies":
include_once("php/Classes/UserArmy.php");
include_once "php/Classes/UserArmyController.php";
include_once "php/Classes/UserArmyView.php";
$ua = new UserArmyView(new UserArmyController($db, $_SESSION['user_id']));
$ua->UserArmiesView();
if(!empty($_GET['id'])) {
$ua->UserArmyView($_GET['id']);
}
break;
...Input? Be honest, I can take it...
