Merge remote-tracking branch 'upstream/main' into correction-tp4
This commit is contained in:
@@ -2,7 +2,31 @@
|
||||
include_once './securite.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 les fichiers de vue une fois les données prêtes.
|
||||
|
||||
@@ -1,28 +1,71 @@
|
||||
<?php
|
||||
include_once __DIR__ . '/connexion.php';
|
||||
|
||||
function getFilms()
|
||||
function getConnection()
|
||||
{
|
||||
static $conn = null;
|
||||
|
||||
if ($conn === null) {
|
||||
$host = getenv('DB_HOST') ?: 'https://dwarves.iut-fbleau.fr';
|
||||
$login = getenv('DB_USER') ?: getenv('USER') ?: getenv('USERNAME') ?: 'root';
|
||||
$password = getenv('DB_PASSWORD') ?: $login;
|
||||
$database = getenv('DB_NAME') ?: $login;
|
||||
$conn = mysqli_connect($host, $login, $password, $database);
|
||||
|
||||
if ($conn !== false) {
|
||||
mysqli_set_charset($conn, 'utf8mb4');
|
||||
}
|
||||
}
|
||||
|
||||
return $conn;
|
||||
}
|
||||
|
||||
function getFilms($page = 1, $perPage = 10)
|
||||
{
|
||||
$conn = getConnection();
|
||||
$sql = "
|
||||
SELECT Film.idFilm, Film.titre, Film.annee, Genre.code AS genre, Artiste.nom, Artiste.prenom
|
||||
FROM Film
|
||||
JOIN Genre ON Genre.code = Film.genre
|
||||
JOIN Artiste ON Artiste.idArtiste = Film.idMes
|
||||
ORDER BY Film.titre ASC
|
||||
";
|
||||
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) {
|
||||
die('Erreur SQL lors de la récupération des films : ' . mysqli_error($conn));
|
||||
return [
|
||||
'films' => [],
|
||||
'total' => 0,
|
||||
'error' => 'Exécution de la requête impossible.',
|
||||
];
|
||||
}
|
||||
|
||||
$films = array();
|
||||
while ($film = mysqli_fetch_assoc($result)) {
|
||||
$films[] = $film;
|
||||
}
|
||||
$films = mysqli_fetch_all($result, MYSQLI_ASSOC);
|
||||
|
||||
mysqli_free_result($result);
|
||||
$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;
|
||||
return [
|
||||
'films' => $films,
|
||||
'total' => $total,
|
||||
'error' => null,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,32 +1,64 @@
|
||||
<!--
|
||||
Variables de la vue
|
||||
$films : les films de la page
|
||||
<!--
|
||||
Variables de la vue
|
||||
$films : les films de la page
|
||||
-->
|
||||
|
||||
<h2>Films</h2>
|
||||
<?php if (!empty($errorMessage)) { ?>
|
||||
<article><?php echo htmlspecialchars($errorMessage, ENT_QUOTES, 'UTF-8'); ?></article>
|
||||
<?php } ?>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Titre</th>
|
||||
<th>Année</th>
|
||||
<th>Genre</th>
|
||||
<th>Réalisateur</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Titre</th>
|
||||
<th>Année</th>
|
||||
<th>Genre</th>
|
||||
<th>Réalisateur</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
foreach($films as $film){
|
||||
|
||||
echo "
|
||||
<tr>
|
||||
<td><a href='#'>{$film['titre']}</a></td>
|
||||
<td>{$film['annee']}</td>
|
||||
<td>{$film['genre']}</td>
|
||||
<td>{$film['prenom']} {$film['nom']}</td>
|
||||
</tr>";
|
||||
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 "
|
||||
<tr>
|
||||
<td><a href='#'>{$titre}</a></td>
|
||||
<td>{$annee}</td>
|
||||
<td>{$genre}</td>
|
||||
<td>{$realisateur}</td>
|
||||
</tr>";
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</tbody>
|
||||
</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 } ?>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user