Browse Source

init

master
Simon Vieille 2 years ago
commit
fc1602a680
8 changed files with 148 additions and 0 deletions
  1. 6
    0
      controllers/error.php
  2. 27
    0
      controllers/person.php
  3. 19
    0
      index.php
  4. 10
    0
      lib/database.php
  5. 39
    0
      models/person.php
  6. 20
    0
      views/person.php
  7. 0
    0
      views/person_add.php
  8. 27
    0
      views/persons.php

+ 6
- 0
controllers/error.php View File

@@ -0,0 +1,6 @@
1
+<?php
2
+
3
+function error404()
4
+{
5
+    echo 'Page non trouvée !';
6
+}

+ 27
- 0
controllers/person.php View File

@@ -0,0 +1,27 @@
1
+<?php
2
+
3
+function persons() 
4
+{
5
+    $persons = getPersons();
6
+
7
+    include 'views/persons.php';
8
+}
9
+
10
+function person() 
11
+{
12
+    if (isset($_GET['id'])) {
13
+        $person = getPerson($_GET['id']);
14
+    } else {
15
+        $person = null;
16
+    }
17
+
18
+    include 'views/person.php';
19
+}
20
+
21
+function personDelete() 
22
+{
23
+    $person = deletePerson($_GET['id']);
24
+
25
+    header('Location: index.php?controller=persons');
26
+    die;
27
+}

+ 19
- 0
index.php View File

@@ -0,0 +1,19 @@
1
+<?php
2
+
3
+require_once 'lib/database.php';
4
+require_once 'models/person.php';
5
+require_once 'controllers/person.php';
6
+require_once 'controllers/error.php';
7
+
8
+if (isset($_GET['controller'])) {
9
+    $controller = $_GET['controller'];
10
+
11
+    if (function_exists($controller)) {
12
+        $controller();
13
+    }
14
+} else {
15
+    error404();
16
+}
17
+
18
+//person();
19
+

+ 10
- 0
lib/database.php View File

@@ -0,0 +1,10 @@
1
+<?php
2
+
3
+function databaseConnection() 
4
+{
5
+    return new Pdo(
6
+        'mysql:dbname=lpwd_person;host=localhost',
7
+        'root',
8
+        'root'
9
+    );
10
+} 

+ 39
- 0
models/person.php View File

@@ -0,0 +1,39 @@
1
+<?php
2
+
3
+function getPersons()
4
+{
5
+    $connection = databaseConnection();
6
+    $query = 'SELECT * FROM person';
7
+
8
+    $request = $connection->prepare($query);
9
+    $request->execute();
10
+
11
+    $results = $request->fetchAll();
12
+
13
+    return $results;
14
+}
15
+
16
+function getPerson($id)
17
+{
18
+    $connection = databaseConnection();
19
+    $query = 'SELECT * FROM person WHERE id = :id';
20
+
21
+    $request = $connection->prepare($query);
22
+    $request->bindParam(':id', $id);
23
+    $request->execute();
24
+
25
+    $results = $request->fetch();
26
+
27
+    return $results;
28
+}
29
+
30
+function deletePerson($id)
31
+{
32
+    $connection = databaseConnection();
33
+    $query = 'DELETE FROM person WHERE id = :id';
34
+
35
+    $request = $connection->prepare($query);
36
+    $request->bindParam(':id', $id);
37
+
38
+    return $request->execute();
39
+}

+ 20
- 0
views/person.php View File

@@ -0,0 +1,20 @@
1
+
2
+<!DOCTYPE html>
3
+<html lang="en">
4
+<head>
5
+    <meta charset="UTF-8">
6
+    <title></title>
7
+</head>
8
+<body>
9
+    <?php if ($person !== null): ?>
10
+        <ul>
11
+            <li>Nom : <?php echo $person['lastname'] ?></li>
12
+            <li>Prénom : <?php echo $person['firstname'] ?></li>
13
+            <li>Âge : <?php echo $person['age'] ?></li>
14
+            <li>Email : <?php echo $person['email'] ?></li>
15
+        </ul>
16
+    <?php else: ?>
17
+        <p>Aucun personne selectionnée.</p>
18
+    <?php endif; ?>
19
+</body>
20
+</html>

+ 0
- 0
views/person_add.php View File


+ 27
- 0
views/persons.php View File

@@ -0,0 +1,27 @@
1
+<!DOCTYPE html>
2
+<html lang="en">
3
+<head>
4
+    <meta charset="UTF-8">
5
+    <title></title>
6
+</head>
7
+<body>
8
+    <h1>Liste des personnes</h1>
9
+
10
+    <ul>
11
+        <?php foreach ($persons as $person): ?>
12
+            <li>
13
+                <ul>
14
+                    <li>Nom : <?php echo $person['lastname'] ?></li>
15
+                    <li>Prénom : <?php echo $person['firstname'] ?></li>
16
+                    <li>Âge : <?php echo $person['age'] ?></li>
17
+                    <li>Email : <?php echo $person['email'] ?></li>
18
+
19
+                    <li>
20
+                        <a href="index.php?controller=personDelete&amp;id=<?php echo $person['id'] ?>">Supprimer</a>
21
+                    </li>
22
+                </ul>
23
+            </li>
24
+        <?php endforeach ?>
25
+    </ul>
26
+</body>
27
+</html>

Loading…
Cancel
Save