@@ -185,23 +185,25 @@ $app->get('/order/{id}', function (Request $request, Response $response, array $ | |||
}); | |||
$app->get('/customers', function (Request $request, Response $response, array $args) { | |||
$db = new myDB(); | |||
$sql = "SELECT * FROM clients"; | |||
$stmt = $db->prepare($sql); | |||
$res = $stmt->execute(); | |||
$response->getBody()->write("<h1>CUSTOMERS LIST </h1>"); | |||
$response->getBody()->write('<hr/>'); | |||
$response->getBody()->write("<table>"); | |||
while ($row = $res->fetchArray(SQLITE3_ASSOC)) { | |||
// maak hier een deftige tabel van | |||
$response->getBody()->write("<tr><td>" . " " . " <a href=\"/orders/" . $row['id'] . "\">" . $row["id"] . "</a> " . " " . $row['firstname'] . " " . $row['lastname'] . " " . $row['street'] . " " . $row['street2'] . " " . $row['zipcode'] . " " . $row['city'] . " " . $row['country'] . "</td></tr>"); | |||
} | |||
$response->getBody()->write("</table>"); | |||
return $response; | |||
}); | |||
$app->get('/customers', \Shop\Controller\Clients::class . ':clients'); | |||
// $app->get('/customers', function (Request $request, Response $response, array $args) { | |||
// $db = new myDB(); | |||
// $sql = "SELECT * FROM clients"; | |||
// $stmt = $db->prepare($sql); | |||
// $res = $stmt->execute(); | |||
// $response->getBody()->write("<h1>CUSTOMERS LIST </h1>"); | |||
// $response->getBody()->write('<hr/>'); | |||
// $response->getBody()->write("<table>"); | |||
// while ($row = $res->fetchArray(SQLITE3_ASSOC)) { | |||
// // maak hier een deftige tabel van | |||
// $response->getBody()->write("<tr><td>" . " " . " <a href=\"/orders/" . $row['id'] . "\">" . $row["id"] . "</a> " . " " . $row['firstname'] . " " . $row['lastname'] . " " . $row['street'] . " " . $row['street2'] . " " . $row['zipcode'] . " " . $row['city'] . " " . $row['country'] . "</td></tr>"); | |||
// } | |||
// $response->getBody()->write("</table>"); | |||
// return $response; | |||
// }); | |||
@@ -0,0 +1,35 @@ | |||
<?php | |||
namespace Shop\Controller; | |||
use Psr\Http\Message\ResponseInterface; | |||
use Psr\Http\Message\ServerRequestInterface; | |||
use Psr\Container\ContainerInterface; | |||
use Shop\DB\Clients as ClientList; | |||
class Clients | |||
{ | |||
protected $container; | |||
protected $twig; | |||
public function __construct(ContainerInterface $container) | |||
{ | |||
$this->container = $container; | |||
} | |||
public function clients(ServerRequestInterface $request, ResponseInterface $response, array $args) | |||
{ | |||
$container = $this->container; | |||
$twig = $container->get('twig'); | |||
$db = new ClientList($this->container); | |||
$clients = $db->getClients(); | |||
$vars = ["clients" => $clients]; | |||
$a = $twig->render('clients.html.twig', $vars); | |||
$response->getBody()->write($a); | |||
return $response; | |||
} | |||
} |
@@ -4,16 +4,18 @@ namespace Shop\DB; | |||
use Shop\DB\myDB; | |||
class CLients extends myDB | |||
class Clients extends myDB | |||
{ | |||
protected $tableName = 'clients'; | |||
protected $container; | |||
public function getClients($tableName) | |||
public function getClients() | |||
{ | |||
$stmt = $this->prepare("SELECT * FROM :tableName;"); | |||
$stmt->bindValue(':tableName', $tableName, SQLITE3_TEXT); | |||
$stmt = $this->prepare("SELECT * FROM clients"); | |||
$res = $stmt->execute(); | |||
return $res; | |||
while($result = $res->fetchArray(SQLITE3_ASSOC)) { | |||
} | |||
return $result; | |||
} | |||
} |
@@ -0,0 +1,18 @@ | |||
<?php | |||
namespace Shop\Model; | |||
class Clients | |||
{ | |||
protected $id; | |||
protected $firstname; | |||
protected $lastname; | |||
protected $company; | |||
protected $vatnumber; | |||
protected $street; | |||
protected $street2; | |||
protected $zipcode; | |||
protected $city; | |||
protected $state; | |||
protected $country; | |||
} |
@@ -9,12 +9,13 @@ class Twig | |||
{ | |||
protected $twig; | |||
protected $variables; | |||
protected $variables; | |||
public function __construct($tmpl_folder) | |||
{ | |||
$loader = new FilesystemLoader($tmpl_folder); | |||
$this->twig = new Environment($loader, []); | |||
$this->variables = []; | |||
} | |||
protected function load($tmpl) | |||
@@ -23,16 +24,16 @@ class Twig | |||
} | |||
// add block variables to the global variable bag | |||
public function addBlockVariable($block, $data) | |||
{ | |||
$current = $this->variables[$block]; | |||
if ($current) { | |||
$new = array_merge($current, $data); | |||
} else { | |||
$new = $data; | |||
} | |||
$this->variables[$block] = $new; | |||
} | |||
// public function addBlockVariable($block, $data) | |||
// { | |||
// $current = $this->variables[$block]; | |||
// if ($current) { | |||
// $new = array_merge($current, $data); | |||
// } else { | |||
// $new = $data; | |||
// } | |||
// $this->variables[$block] = $new; | |||
// } | |||
public function render($tmpl, $vars) | |||
{ |
@@ -0,0 +1,37 @@ | |||
{% extends "base.html.twig" %} {% block main %} | |||
<h1>CUSTOMER LIST</h1> | |||
<hr /> | |||
<table> | |||
<tr> | |||
<th>id</th> | |||
<th>First Name</th> | |||
<th>Last Name</th> | |||
<th>Company</th> | |||
<th>VAT Number</th> | |||
<th>Street</th> | |||
<th>Street 2</th> | |||
<th>ZIP Code</th> | |||
<th>City</th> | |||
<th>State</th> | |||
<th>Country</th> | |||
</tr> | |||
{% for client in clients%} | |||
<tr> | |||
<td> | |||
<a href="/orders/{{ client.id }}">{{ client.id }}</a> | |||
</td> | |||
<td>{{ client.firstname }}</td> | |||
<td>{{ client.lastname }}</td> | |||
<td>{{ client.company }}</td> | |||
<td>{{ client.vatnumber }}</td> | |||
<td>{{ client.street }}</td> | |||
<td>{{ client.street2 }}</td> | |||
<td>{{ client.zipcode }}</td> | |||
<td>{{ client.city }}</td> | |||
<td>{{ client.state }}</td> | |||
<td>{{ client.country }}</td> | |||
</tr> | |||
{% endfor %} | |||
</table> | |||
{% endblock %} |