<?phperror_reporting(E_ALL);ini_set('display_errors', 1);require_once 'Credentials/credentials.php';require_once ("Entities/CoffeeEntity.php");$coffee = new CoffeeModel($db);var_dump( $coffee->GetCoffeeTypes() );var_dump( $coffee->GetCoffeeByType('sometype') );require_once ("Entities/CoffeeEntity.php");//Contains database related code for the Coffee page.class CoffeeModel { private $db; public function __construct(\PDO $db) { $this->db = $db; } public function GetCoffeeTypes() { $stmt = $this->db->query("SELECT DISTINCT type FROM coffee"); return $stmt->fetchAll(); } public function GetCoffeeByType($type) { $stmt = $this->db->prepare("SELECT name, type, price, roast, country, image, review FROM coffee WHERE type LIKE :type"); $stmt->execute(['%' . $type . '%']); return $stmt->fetchAll(PDO::FETCH_CLASS, 'CoffeeEntity'); }}?>
<?phprequire_once 'Controller/CoffeeController.php';$coffeeController = new CoffeeController();if(isset($_POST['types'])){ //Fill page with coffees of the selected type $coffeeTables = $coffeeController->CreateCoffeeTables($_POST['types']);}else { //Page is loaded for the first time, no type selected -> Fetch all types $coffeeTables = $coffeeController->CreateCoffeeTables('%');}//Output page data$title = 'Coffee overview';$content = $coffeeController->CreateCoffeeDropdownList(). $coffeeTables;include 'Template.php';?>
<?phperror_reporting(E_ALL);ini_set('display_errors', 1);require_once 'Credentials/credentials.php';require_once ("Entities/CoffeeEntity.php");$coffee = new CoffeeModel($db);var_dump( $coffee->GetCoffeeTypes() );var_dump( $coffee->GetCoffeeByType('sometype') );require_once ("Entities/CoffeeEntity.php");//Contains database related code for the Coffee page.class CoffeeModel { private $db; public function __construct(\PDO $db) { $this->db = $db; } public function GetCoffeeTypes() { $stmt = $this->db->query("SELECT DISTINCT type FROM coffee"); return $stmt->fetchAll(); } public function GetCoffeeByType($type) { $stmt = $this->db->prepare("SELECT name, type, price, roast, country, image, review FROM coffee WHERE type LIKE :type"); $stmt->execute(['%' . $type . '%']); return $stmt->fetchAll(PDO::FETCH_CLASS, 'CoffeeEntity'); }}?>
NB NB NB NB - this is not the solution - I don't know what or where your DB connection is defined in your code - you must replace $db in the above line with whatever variable you are storing that PDO db connection.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
According to your CoffeeModel class you have to inject a db parameter - it is required.
When you create your CoffeeModel you need to give it a PDO connection
Open in new window