Very new to web programming still and I don't really have a specific question per se, but I was wondering how close this design is to how an MVC would be implemented under these conditions. Here is the template class that I am using. db.php
just connects to the database.
I'd like to stay away from answers like "Use CodeIgniter, CakePHP, Zend Framework, et cetera" because I most likely am going to end up doing that eventually but for now I'd like to understand how to implement a barebones MVC pattern in a typical website using out of the box PHP and HTML. Also, I've read the wikipedia page for Model-view-controller and I am still confused as to how to apply it in this circumstance.
I don't really like the solution I am using right now because it still seems rather unorganized. I'm not sure what exactly I mean but it just seems to smell a bit. With the mass amount of variables it still seems rather messy and inelegant. It seems like many parts of the site struggle for access to other parts and that the overall design is a bit mucky. I am constantly passing large amounts of variables to constructors and the entire solution seems very inflexible. I don't know; I'm still somewhat confused as to what the controller is in this context or whether or not I even have one. Hopefully someone can alleviate all this confusion x_x
index.php
<?php
require('db.php');
require('header_model.php');
require('submissions_model.php');
require('template.php');
$headerModel = new HeaderModel();
$page = $headerModel->getPage();
$sort = $headerModel->getSort();
$search = $headerModel->getSearch();
$submissionsModel = new SubmissionsModel($sort, $page, $resultsPerPage, $search);
$submissions = $submissionsModel->getSubmissions();
$outcomeCount = $submissionsModel->getOutcomeCount();
$index_view = new Template('index_view.php', array(
'header' => new Template('header.php'),
'menu' => new Template('menu.php', array('sort' => $sort)),
'submissions' => new Template('submissions.php', array('submissions' => $submissions)),
'pagination' => new Template('pagination.php', array('page' => $page, 'resultsPerPage' => $resultsPerPage, 'outcomeCount' => $outcomeCount, 'sort' => $sort)),
'footer' => new Template('footer.php')
));
$index_view->render();
?>
index_view.php snippet
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Website</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="styles.css" rel="stylesheet" type="text/css"/>
<link href="favicon.png" rel="shortcut icon" />
</head>
<body>
<?php
$this->header->render();
$this->menu->render();
$this->submissions->render();
$this->pagination->render();
$this->footer->render();
?>
</body>
</html>
header_model.php snippet
public function getSearch() {
if (isset($_GET['search']))
return $_GET['search'];
else
return '';
}
submissions_model.php snippet
class SubmissionsModel {
private $sort;
private $page;
private $resultsPerPage;
private $search;
public $submissions;
public $outcomeCount;
public function __construct($sort, $page, $resultsPerPage, $search) {
$this->sort = $sort;
$this->page = $page;
$this->resultsPerPage = $resultsPerPage;
$this->search = $search;
$this->initialize();
}
private function initialize() {
$submissionQuery = $this->getSubmissionQuery($this->search);
$this->outcomeCount = mysql_num_rows($submissionQuery);
$this->submissions = array();
while ($row = mysql_fetch_assoc($submissionQuery)) {
$voteblock = $this->getVoteblock($row);
$tags = $this->getTags($row);
$commentCount = mysql_num_rows(mysql_query("SELECT id FROM comments WHERE submissionID = $row[id]"));
$this->submissions[] = array('submission' => $row, 'upvote' => $voteblock['upvote'], 'votes' => $voteblock['votes'], 'downvote' => $voteblock['downvote'], 'tags' => $tags, 'commentCount' => $commentCount);
}
}
}