So I decided to code my own MVC and chose to follow along with the JReam video tutorials. I have come to a roadblock. I cannot get data from my database to display on my view. Here is my file structure and code.
website
/controllers
/home.php
/core
/Bootstrap.php
/Controller.php
/Database.php
/Model.php
/Session.php
/View.php
/media
/models
/home_model.php
/public
/views
/home_view.php
.htaccess
config.php
index.php
index.php code:
<?php
require_once ('config.php');
// check development setting and display errors appropriately
function setReporting() {
if (DEVELOPMENT_ENVIRONMENT == true) {
error_reporting(E_ALL);
ini_set('display_errors','On');
} else {
error_reporting(E_ALL);
ini_set('display_errors','Off');
ini_set('log_errors', 'On');
//ini_set('error_log', ROOT.DS.'tmp'.DS.'logs'.DS.'error.log');
}
}
// check for magic quotes and remove them
function stripSlashesDeep($value) {
$value = is_array($value) ? array_map('stripSlashesDeep', $value) : stripslashes($value);
return $value;
}
function removeMagicQuotes() {
if (get_magic_quotes_gpc()) {
$_GET = stripSlashesDeep($_GET);
$_POST = stripSlashesDeep($_POST);
$_COOKIE = stripSlashesDeep($_COOKIE);
}
}
// check register globals and remove them
function unregisterGlobals() {
if (ini_get('register_globals')) {
$array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
foreach ($array as $value) {
foreach ($GLOBALS[$value] as $key => $var) {
if ($var === $GLOBALS[$key]) {
unset($GLOBALS[$key]);
}
}
}
}
}
// call functions
setReporting();
removeMagicQuotes();
unregisterGlobals();
// require core files
require_once (CORE_PATH . 'Bootstrap.php');
require_once (CORE_PATH . 'Controller.php');
require_once (CORE_PATH . 'Model.php');
require_once (CORE_PATH . 'View.php');
require_once (CORE_PATH . 'Database.php');
require_once (CORE_PATH . 'Session.php');
// instantiate new bootstrap class
$app = new Bootstrap();
Bootstrap.php code:
class Bootstrap {
function __construct() {
// grab url and explode it
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = explode('/', $url);
// load home controller if no url present
if (empty($url[0])) {
require (CONTROLLER_PATH . 'home.php');
$controller = new Home();
$controller->index();
return false;
}
// load the controller if it exists in the url
$file = CONTROLER_PATH . $url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
$this->error();
}
$controller = new $url[0];
$controller->loadModel($url[0]);
// load method it exists in the url
if (isset($url[2])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}($url[2]);
} else {
$this->error();
}
} else {
if (isset($url[1])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->error();
}
} else {
$controller->index();
}
}
}
// load error controller if url controller does not exist
function error() {
require (CONTROLLER_PATH . 'error.php');
$controller = new Error();
$controller->index();
return false;
}
}
Controller.php code:
class Controller
{
function __construct() {
$this->view = new View();
}
public function loadModel($name) {
$path = MODEL_PATH . $name . '_model.php';
if (file_exists($path)) {
require_once (MODEL_PATH . $name . '_model.php');
$modelName = $name . '_Model';
$this->model = new $modelName();
}
}
}
Model.php code:
class Model {
public function __construct() {
$this->db = new Database();
// get page information here
}
}
View.php code:
class View
{
function __construct() {
// constructor does nothing
}
public function render($name) {
require_once (VIEW_PATH . $name . '.php');
}
}
Database.php code:
class Database extends PDO
{
public function __construct() {
parent::__construct(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
}
}
Now for my home page stuff...
home controller code:
class Home extends Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->view->render('home_view');
}
public function randomVideos() {
$data['randomVideos'] = $this->model->randomVideos();
}
}
home_view code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=SITE_NAME?>></title>
<meta name="Keywords" content="" />
<meta name="Description" content="" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="<?=CSS_PATH?>global.css" type="text/css" />
<script type="text/javascript" src="<?=FLOWPLAYER_PATH?>flowplayer-3.2.6.min.js"></script>
<script type="text/javascript" src="<?=JS_PATH?>jquery_1.6.1.js"></script>
</head>
<body>
<!-- wrap -->
<div id="wrap">
<?php include ('header_view.php'); ?>
<?php include ('menu_view.php'); ?>
<!-- content-wrap -->
<div id="content-wrap" class="two-col">
<?php include('sidebar_view.php'); ?>
<!-- main content -->
<div id="main">
<h1></h1>
<!-- flowplayer -->
<a
href="../media/preview.mov"
style="display:block; width:425px; height:300px; margin:0 auto; margin-top:10px; margin-bottom:10px;"
id="player">
</a>
<script language="JavaScript">
flowplayer("player", "../public/flowplayer/flowplayer-3.2.7.swf", {
version: [9, 115],
plugins: {
sharing: {
url: '../public/flowplayer/flowplayer.sharing-3.2.1.swf',
buttons: {
overColor: '#0099FF'
},
facebook: false
}
},
clip: {
autoPlay: true,
onStart: function (clip) {
var w = parseInt(clip.metaData.width, 10),
h = parseInt(clip.metaData.height, 10);
$(this.getParent()).css({width: w, height: h});
}
}
});
</script>
<!-- end flowplayer -->
<?php print_r($data['randomVideos']); ?>
<?php include ('banner_ad_view.php'); ?>
<!-- end main -->
</div>
<!-- end content-wrap -->
</div>
<?php include ('footer_view.php'); ?>
<!-- end wrap -->
</div>
</body>
</html>
home_model code:
class Home_Model extends Model
{
public function __construct() {
parent::__construct();
}
public function randomVideos() {
$stmt = $this->db->prepare("SELECT video_title, video_photo, video_model FROM video WHERE video_delete = 0 ORDER BY RAND() LIMIT 0, 12");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
return $stmt->fetchAll();
}
}