init
This commit is contained in:
當前提交
fc1602a680
6
controllers/error.php
Normal file
6
controllers/error.php
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
function error404()
|
||||
{
|
||||
echo 'Page non trouvée !';
|
||||
}
|
27
controllers/person.php
Normal file
27
controllers/person.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
function persons()
|
||||
{
|
||||
$persons = getPersons();
|
||||
|
||||
include 'views/persons.php';
|
||||
}
|
||||
|
||||
function person()
|
||||
{
|
||||
if (isset($_GET['id'])) {
|
||||
$person = getPerson($_GET['id']);
|
||||
} else {
|
||||
$person = null;
|
||||
}
|
||||
|
||||
include 'views/person.php';
|
||||
}
|
||||
|
||||
function personDelete()
|
||||
{
|
||||
$person = deletePerson($_GET['id']);
|
||||
|
||||
header('Location: index.php?controller=persons');
|
||||
die;
|
||||
}
|
19
index.php
Normal file
19
index.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
require_once 'lib/database.php';
|
||||
require_once 'models/person.php';
|
||||
require_once 'controllers/person.php';
|
||||
require_once 'controllers/error.php';
|
||||
|
||||
if (isset($_GET['controller'])) {
|
||||
$controller = $_GET['controller'];
|
||||
|
||||
if (function_exists($controller)) {
|
||||
$controller();
|
||||
}
|
||||
} else {
|
||||
error404();
|
||||
}
|
||||
|
||||
//person();
|
||||
|
10
lib/database.php
Normal file
10
lib/database.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
function databaseConnection()
|
||||
{
|
||||
return new Pdo(
|
||||
'mysql:dbname=lpwd_person;host=localhost',
|
||||
'root',
|
||||
'root'
|
||||
);
|
||||
}
|
39
models/person.php
Normal file
39
models/person.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
function getPersons()
|
||||
{
|
||||
$connection = databaseConnection();
|
||||
$query = 'SELECT * FROM person';
|
||||
|
||||
$request = $connection->prepare($query);
|
||||
$request->execute();
|
||||
|
||||
$results = $request->fetchAll();
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
function getPerson($id)
|
||||
{
|
||||
$connection = databaseConnection();
|
||||
$query = 'SELECT * FROM person WHERE id = :id';
|
||||
|
||||
$request = $connection->prepare($query);
|
||||
$request->bindParam(':id', $id);
|
||||
$request->execute();
|
||||
|
||||
$results = $request->fetch();
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
function deletePerson($id)
|
||||
{
|
||||
$connection = databaseConnection();
|
||||
$query = 'DELETE FROM person WHERE id = :id';
|
||||
|
||||
$request = $connection->prepare($query);
|
||||
$request->bindParam(':id', $id);
|
||||
|
||||
return $request->execute();
|
||||
}
|
20
views/person.php
Normal file
20
views/person.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
<?php if ($person !== null): ?>
|
||||
<ul>
|
||||
<li>Nom : <?php echo $person['lastname'] ?></li>
|
||||
<li>Prénom : <?php echo $person['firstname'] ?></li>
|
||||
<li>Âge : <?php echo $person['age'] ?></li>
|
||||
<li>Email : <?php echo $person['email'] ?></li>
|
||||
</ul>
|
||||
<?php else: ?>
|
||||
<p>Aucun personne selectionnée.</p>
|
||||
<?php endif; ?>
|
||||
</body>
|
||||
</html>
|
0
views/person_add.php
Normal file
0
views/person_add.php
Normal file
27
views/persons.php
Normal file
27
views/persons.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Liste des personnes</h1>
|
||||
|
||||
<ul>
|
||||
<?php foreach ($persons as $person): ?>
|
||||
<li>
|
||||
<ul>
|
||||
<li>Nom : <?php echo $person['lastname'] ?></li>
|
||||
<li>Prénom : <?php echo $person['firstname'] ?></li>
|
||||
<li>Âge : <?php echo $person['age'] ?></li>
|
||||
<li>Email : <?php echo $person['email'] ?></li>
|
||||
|
||||
<li>
|
||||
<a href="index.php?controller=personDelete&id=<?php echo $person['id'] ?>">Supprimer</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue