I am trying to display the database on the database.phtml
page of my website. This page is a view.
So far I have a model called DatabaseConect.php
<?php
abstract class DatabaseConect {
protected $db = NULL;
public function __construct (PDO $db) {
$this->db = $db;
}
}
class Database extends DatabaseConect {
public $props = array ();
private function getPage ($id) {
$q = $this->db->prepare('SELECT * FROM retrofootball_products');
$ret = $res->fetchAll();
return ($this->props=$ret);
}
}
$db = new PDO('mysql:host=helios.csesalford.com;dbname=pd12', 'helloworld', 'password'); //I have changed the log in details
Then in my view
<?php require('template/header.phtml') ?>
<?php
$page = new Database ($db);
?>
<?php require('template/footer.phtml') ?>
I have been trying to find stuff on Stackoverflow and Have come across these articles but they are little above my head as I am new to this:
Properly calling the database from Model in an MVC application? http://programmers.stackexchange.com/questions/178831/using-pdo-with-mvc
My question is what is the best way using MVC to connect to a database and then display this in the view? Do I need to use PDO::FETCH
in the model to display the results into a variable and then call this variable in the view?
EDIT: Like suggest I have used a bootstrap in a controller. Do i need to create a new instance of this in my view for it to work? Also where am i running the query in the correct place?
<?php
class Dependency_Manager {
private $db;
public function __construct($settings) {
$this->db = new PDO('mysql:host=helios.csesalford.com;dbname=helloworld', 'password', 'php54');
}
public function getDB() {
return $db;
}
}
class CMS {
public function __construct(PDO $db) {
//$stmt = $db->query('SELECT * FROM retrofootball_products');
}
}
$settings = array();
$dm = new Dependency_Manager($settings);
$cms = new CMS($dm->getDB());