<?php
error_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');
}
}
?>
<?php
require_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';
?>
<?php
require_once ("Model/CoffeeModel.php");
//Contains non-database related function for the Coffee page
class CoffeeController {
function CreateCoffeeDropdownList() {
$coffeeModel = new CoffeeModel();
$result = "<form action = '' method = 'post' width = '200px'>
Please select a type:
<select name = 'types' >
<option value = '%' >All</option>
" . $this->CreateOptionValues($coffeeModel->GetCoffeeTypes()) .
"</select>
<input type = 'submit' value = 'Search' />
</form>";
return $result;
}
function CreateOptionValues(array $valueArray) {
$result = "";
foreach ($valueArray as $value) {
$result = $result . "<option value='$value'>$value</option>";
}
return $result;
}
function CreateCoffeeTables($types)
{
$coffeeModel = new CoffeeModel();
$coffeeArray = $coffeeModel->GetCoffeeByType($types);
$result = "";
//Generate a coffeeTable for each coffeeEntity in array
foreach ($coffeeArray as $key => $coffee)
{
$result = $result .
"<table class = 'coffeeTable'>
<tr>
<th rowspan='6' width = '150px' ><img runat = 'server' src = '$coffee->image' /></th>
<th width = '75px' >Name: </th>
<td>$coffee->name</td>
</tr>
<tr>
<th>Type: </th>
<td>$coffee->type</td>
</tr>
<tr>
<th>Price: </th>
<td>$coffee->price</td>
</tr>
<tr>
<th>Roast: </th>
<td>$coffee->roast</td>
</tr>
<tr>
<th>Origin: </th>
<td>$coffee->country</td>
</tr>
<tr>
<td colspan='2' >$coffee->review</td>
</tr>
</table>";
}
return $result;
}
}
?>
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE