Part on PDO first steps finished
This commit is contained in:
parent
8bb5f373f8
commit
0806411f09
1 changed files with 11 additions and 19 deletions
34
index.php
34
index.php
|
|
@ -1,26 +1,18 @@
|
||||||
<?php
|
<?php
|
||||||
require 'functions.php';
|
require 'functions.php';
|
||||||
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
|
|
||||||
|
|
||||||
$routes = [
|
//require 'router.php';
|
||||||
'/' => 'controllers/index.php',
|
// connect to MySQL database.
|
||||||
'/about' => 'controllers/about.php',
|
$dsn = "mysql:host=localhost;port=3306;dbname=myapp;charset=utf8mb4";
|
||||||
'/contact' => 'controllers/contact.php',
|
$username = 'appUser';
|
||||||
'/mission' => 'controllers/mission.php',
|
$password = 'password';
|
||||||
];
|
$pdo = new PDO($dsn, $username, $password);
|
||||||
|
|
||||||
function routeToController($uri, $routes) {
|
$statement = $pdo->prepare("SELECT * FROM posts");
|
||||||
if (array_key_exists($uri, $routes)) {
|
$statement->execute();
|
||||||
require $routes[$uri];
|
|
||||||
} else {
|
$posts = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||||
abort();
|
|
||||||
}
|
foreach ($posts as $post) {
|
||||||
}
|
echo "<li>" . $post['title'] . "</li>";
|
||||||
|
|
||||||
routeToController($uri, $routes);
|
|
||||||
|
|
||||||
function abort($code = 404) {
|
|
||||||
http_response_code($code);
|
|
||||||
require "views/{$code}.php";
|
|
||||||
die();
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue