Well I'm new to MVC and am trying to write a site in it. The site is a basic flash games site and has categories to track the games. My first step was to create a basic MVC setup and list all the categories. The problem is I don't know how to create an array with all of the categories in it. Can I add this to the controller? Should I be adding a new method to the model? Let me know, code is below.
index.php (controller)
<?php
require_once 'database.php';
require_once 'models/category.php';
if (isset($_GET['page']))
{
}
else
{ //display home page
require_once 'views/home.php';
}
?>
models/category.php (model)
<?php
class Category
{
private $id,$name;
function __construct($id)
{
$category_query = $database_connection->prepare('SELECT id,name FROM categories WHERE id=?');
$category_query->execute(array($id));
$category = $category_query->fetch();
$this->id = $category['id'];
$this->name = $category['name'];
}
function get_id()
{
return $this->id;
}
function get_name()
{
return $this->name;
}
}
?>
views/home.php (view)
List of all categories:
<ul>
<?php foreach ($all_categories as $category) { ?>
<li><?php echo $category->get_name(); ?> (ID: <?php echo $category->get_id(); ?>)</li>
<?php } ?>
</ul>