correction ex2

This commit is contained in:
JARNOUEN DE VILLARTAY Ulysse (SAFRAN AIRCRAFT ENGINES)
2026-04-08 16:10:49 +02:00
parent cf7a51459c
commit 09e55a2b28
3 changed files with 146 additions and 39 deletions
+29 -6
View File
@@ -1,14 +1,37 @@
<?php <?php
include './modeles/modeleFilms.php'; include_once './modeles/modeleFilms.php';
$films = getFilms(); $currentPage = filter_input(
INPUT_GET,
'page',
FILTER_VALIDATE_INT,
['options' => ['min_range' => 1]]
);
if ($currentPage === null || $currentPage === false) {
$currentPage = 1;
}
$perPage = 10;
$data = getFilms($currentPage, $perPage);
$films = $data['films'];
$errorMessage = $data['error'];
$totalFilms = $data['total'];
$totalPages = max(1, (int) ceil($totalFilms / $perPage));
if ($currentPage > $totalPages && $totalFilms > 0) {
$currentPage = $totalPages;
$data = getFilms($currentPage, $perPage);
$films = $data['films'];
$errorMessage = $data['error'];
$totalFilms = $data['total'];
}
// //
// on "charge" la vue // on "charge" la vue
// //
include './vues/header.php'; include_once './vues/header.php';
include './vues/vueFilms.php'; include_once './vues/vueFilms.php';
include './vues/footer.php'; include_once './vues/footer.php';
?>
@@ -1,16 +1,68 @@
<?php <?php
function _getConnection() function getConnection()
{ {
static $_conn = NULL; static $conn = null;
if ($_conn === NULL){
$_conn = mysqli_connect("localhost","","",""); if ($conn === null) {
$login = getenv('DB_USER') ?: getenv('USER') ?: getenv('USERNAME') ?: 's576807';
$password = getenv('DB_PASSWORD') ?: $login;
$database = getenv('DB_NAME') ?: $login;
$conn = mysqli_connect('localhost', $login, $password, $database);
if ($conn !== false) {
mysqli_set_charset($conn, 'utf8mb4');
} }
return $_conn;
} }
return $conn;
}
function getFilms() function getFilms($page = 1, $perPage = 10)
{ {
// A completer $conn = getConnection();
if ($conn === false) {
return [
'films' => [],
'total' => 0,
'error' => 'Connexion MySQL impossible.',
];
}
$page = max(1, (int) $page);
$perPage = max(1, (int) $perPage);
$offset = ($page - 1) * $perPage;
$sql = "SELECT SQL_CALC_FOUND_ROWS
F.idFilm,
F.titre,
F.annee,
G.code AS genre,
A.prenom,
A.nom
FROM Film AS F
INNER JOIN Artiste AS A ON F.idMes = A.idArtiste
INNER JOIN Genre AS G ON F.genre = G.code
ORDER BY F.titre ASC
LIMIT {$offset}, {$perPage}";
$result = mysqli_query($conn, $sql);
if ($result === false) {
return [
'films' => [],
'total' => 0,
'error' => 'Exécution de la requête impossible.',
];
}
$films = mysqli_fetch_all($result, MYSQLI_ASSOC);
$totalResult = mysqli_query($conn, 'SELECT FOUND_ROWS() AS total');
$totalRow = $totalResult ? mysqli_fetch_assoc($totalResult) : ['total' => 0];
$total = isset($totalRow['total']) ? (int) $totalRow['total'] : 0;
return [
'films' => $films,
'total' => $total,
'error' => null,
];
} }
?>
+37 -5
View File
@@ -4,6 +4,9 @@
--> -->
<h2>Films</h2> <h2>Films</h2>
<?php if (!empty($errorMessage)) { ?>
<article><?php echo htmlspecialchars($errorMessage, ENT_QUOTES, 'UTF-8'); ?></article>
<?php } ?>
<table> <table>
<thead> <thead>
<tr> <tr>
@@ -16,17 +19,46 @@
<tbody> <tbody>
<?php <?php
foreach ($films as $film) { foreach ($films as $film) {
$titre = htmlspecialchars($film['titre'], ENT_QUOTES, 'UTF-8');
$annee = htmlspecialchars((string) $film['annee'], ENT_QUOTES, 'UTF-8');
$genre = htmlspecialchars($film['genre'], ENT_QUOTES, 'UTF-8');
$realisateur = htmlspecialchars($film['prenom'] . ' ' . $film['nom'], ENT_QUOTES, 'UTF-8');
echo " echo "
<tr> <tr>
<td><a href='#'>{$film['titre']}</a></td> <td><a href='#'>{$titre}</a></td>
<td>{$film['annee']}</td> <td>{$annee}</td>
<td>{$film['genre']}</td> <td>{$genre}</td>
<td>{$film['prenom']} {$film['nom']}</td> <td>{$realisateur}</td>
</tr>"; </tr>";
} }
?> ?>
</tbody> </tbody>
</table> </table>
<?php if ($totalPages > 1) { ?>
<nav>
<ul>
<li>
<?php if ($currentPage > 1) { ?>
<a href="?page=<?php echo $currentPage - 1; ?>">Précédent</a>
<?php } else { ?>
<span>Précédent</span>
<?php } ?>
</li>
</ul>
<ul>
<li>Page <?php echo $currentPage; ?> / <?php echo $totalPages; ?></li>
</ul>
<ul>
<li>
<?php if ($currentPage < $totalPages) { ?>
<a href="?page=<?php echo $currentPage + 1; ?>">Suivant</a>
<?php } else { ?>
<span>Suivant</span>
<?php } ?>
</li>
</ul>
</nav>
<?php } ?>