diff --git a/R3.01/tp/tp3/cinema/src/films.php b/R3.01/tp/tp3/cinema/src/films.php index b9563da..f85002d 100644 --- a/R3.01/tp/tp3/cinema/src/films.php +++ b/R3.01/tp/tp3/cinema/src/films.php @@ -1,14 +1,37 @@ ['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 './vues/vueFilms.php'; -include './vues/footer.php'; -?> +include_once './vues/header.php'; +include_once './vues/vueFilms.php'; +include_once './vues/footer.php'; diff --git a/R3.01/tp/tp3/cinema/src/modeles/modeleFilms.php b/R3.01/tp/tp3/cinema/src/modeles/modeleFilms.php index 1ca9f2e..0fce5de 100644 --- a/R3.01/tp/tp3/cinema/src/modeles/modeleFilms.php +++ b/R3.01/tp/tp3/cinema/src/modeles/modeleFilms.php @@ -1,16 +1,68 @@ [], + '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, + ]; } -?> diff --git a/R3.01/tp/tp3/cinema/src/vues/vueFilms.php b/R3.01/tp/tp3/cinema/src/vues/vueFilms.php index 6d40687..93f70dd 100644 --- a/R3.01/tp/tp3/cinema/src/vues/vueFilms.php +++ b/R3.01/tp/tp3/cinema/src/vues/vueFilms.php @@ -1,32 +1,64 @@ -
| Titre | -Année | -Genre | -Réalisateur | -
|---|---|---|---|
| Titre | +Année | +Genre | +Réalisateur | +{$film['titre']} | -{$film['annee']} | -{$film['genre']} | -{$film['prenom']} {$film['nom']} | - "; +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 " +
| {$titre} | +{$annee} | +{$genre} | +{$realisateur} | +