Menu
Hi there,
I’ve been trying to learn OOP and I get it I think but I really want to understand the MVC way of OOP as it seems to make a lot of sense to use that, but I generally don’t understand how to separate them completely. So I was hoping someone (like nog_dog who seems to know everything i want to) could point me in the direction of something thoroughly explaining MVC and how to plan for this approach in a project.
Thanks much in advance!
[code=php]
<?
// Always start session first
session_start();
// Setup variables
include("./includes/variables.php");
include($admininc."functions.php");
// Connect to database
$db = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if( mysqli_connect_errno() ) {
echo "Connection to database failed: ".mysqli_connect_errno();
exit;
}
// include content file
if ( ISSET($_GET['action']) ) {
$action = trim($_GET['action']);
if( $action == "logout" ) {
unset($_SESSION['userid']);
session_destroy();
$content .= "You have been logged out.<br />n";
header("refresh:2;url={$adminurl}");
} else {
// User Session
$login = checkLogin();
if ( ISSET($_SESSION['userid']) ) {
if( ISSET($_GET['page']) ) $page = $_GET['page'];
include("{$admininc}{$action}.php");
} else {
$content .= "You must be logged in to view this area.<br />";
}
}
} else {
// User Session
$login = checkLogin();
if ( ISSET($_SESSION['userid']) ) {
$content .= "You are logged in, please select an action.";
} else {
$content .= "You must be logged in to view this area.";
}
}
$db->close;
// Print page
include($admininc."header.php");
include($admininc."nav.php");
echo "<div id="main">n{$content}</div>n";
include($admininc."footer.php");
[/code]
[code=php]
switch($page) {
case "gal":
// Add Gallery Form
$content .= "<h1>Add Gallery</h1>
<form action="{$adminurl}?action=gallery&page=galsub" method="post">
<label for="galname">New Gallery Name</label><input type="text" name="galname" />
<label for="submit"> </label><input type="submit" name="submit" value="Submit" /><input type="reset" value="Reset" />";
break;
case "galsub":
// Add Gallery Processing
$galname = sqlSafe($_POST['galname']);
$galsql = "INSERT INTO {$dbgallerytable} (idgallery,gallery_name) VALUES (NULL,'{$galname}')";
if( $db->query($galsql) ) {
$content .= "<p>$galname was added to the gallery listing, please make sure to add an album and some pictures.</p>";
} else {
$content .= "<p>Gallery: $galname failed to be added.</p>";
}
break;
}
[/code]
[code=php]
//controller
include("recentpost.php");
$posts = recentpost($id,$duration);
include("postlayout.php");
postLayout($posts);
//model
function recentpost($id,$duration) {
GLOBAL $db;
$lasttime = time()-$duration;
$sql = "SELECT * FROM post WHERE user=$id AND postdate>$lasttime";
$result = $db->query($sql);
foreach($result as $key=>$value) {
$post[$key] = $value;
}
return $post;
}
// view
function postLayout($posts) {
echo "<table>";
foreach( $post as $key => $value) {
echo "<tr><td>$key:</td><td>$value</td></tr>";
}
echo "</table>";
}[/code]
[code=php]
<?php
// leaving out general config/connect stuff, class auto-loader, etc....
$ctrl = new Controller();
?>
[/code]
[code=php]
<?php
class Controller
{
public function __construct()
{
if(!empty($_GET['user'])) {
$this->getUserInfo();
}
else {
include 'default_view.php';
}
}
public function getUserInfo()
{
$user = new UserModel();
$data['user'] = $user->getUserArray((int)$_GET['user']);
include 'view_user.php';
}
}
?>
[/code]
[code=php]
<?php
class UserModel
{
public function getUserArray($userId)
{
// stuff here to query DB for $userId and populate array $data
return $data;
}
}
?>
[/code]
[code=php]
<html>
<head><title>View User: <?php echo $data['user']['name']; ?></title></head>
<body>
<h1>Info for <?php echo $data['user']['name']; ?></h1>
<ul>
<li>Age: <?php echo $data['user']['age']; ?></li>
<!-- and so forth... -->
</ul>
</body></html>
[/code]
0.1.9 — BETA 5.6