correction-ulysse (#2)

# Correction pour les TP1 TP2 et TP3

Je n'ai pas testé la connexion à la DB pour le moment car je ne peux pas me co sur https://dwarves.iut-fbleau.fr/phpmyadmin

Co-authored-by: JARNOUEN DE VILLARTAY Ulysse (SAFRAN AIRCRAFT ENGINES) <ulysse.jarnouen-de-villartay@safrangroup.com>
Reviewed-on: monnerat/web_2025#2
This commit is contained in:
2026-04-15 15:34:46 +02:00
parent 045ec81ae2
commit a4580652f8
22 changed files with 692 additions and 215 deletions
+3
View File
@@ -8,3 +8,6 @@ color:#2E8B57;
.Loss{
color:#FF6347;
}
.Draw{
color:#DAA520;
}
+29 -14
View File
@@ -1,20 +1,35 @@
<?php
$images = ["rock.png","paper.png","scissors.png"];
$isPlaying = true;
$ROCK = 0;
$PAPER = 1;
$SCISSORS = 2;
$playerChoice = 0;
$computerChoice = 1;
$images = ["rock.png", "paper.png", "scissors.png"];
$rules = [
$ROCK => [$ROCK => "Draw", $PAPER => "Loss", $SCISSORS => "Win"],
$PAPER => [$ROCK => "Win", $PAPER => "Draw", $SCISSORS => "Loss"],
$SCISSORS => [$ROCK => "Loss", $PAPER => "Win", $SCISSORS => "Draw"],
];
$message = "Win";
$class = "Win";
$playerChoice = filter_input(
INPUT_GET,
'choice',
FILTER_VALIDATE_INT,
['options' => ['min_range' => 0, 'max_range' => 2]]
);
$imagePlayer = $images[$playerChoice];
$imageComputer = $images[$computerChoice];
$isPlaying = ($playerChoice !== null && $playerChoice !== false);
include './views/header.php';
if ($isPlaying){
include './views/game.php';
include './views/message.php';
if ($isPlaying) {
$computerChoice = mt_rand(0, 2);
$message = $rules[$playerChoice][$computerChoice];
$class = $message;
$imagePlayer = $images[$playerChoice];
$imageComputer = $images[$computerChoice];
}
include './views/footer.php';
?>
include_once './views/header.php';
if ($isPlaying) {
include_once './views/game.php';
include_once './views/message.php';
}
include_once './views/footer.php';
+30 -7
View File
@@ -1,14 +1,37 @@
<?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 './vues/vueFilms.php';
include './vues/footer.php';
?>
include_once './vues/header.php';
include_once './vues/vueFilms.php';
include_once './vues/footer.php';
+63 -10
View File
@@ -1,16 +1,69 @@
<?php
function _getConnection()
function getConnection()
{
static $_conn = NULL;
if ($_conn === NULL){
$_conn = mysqli_connect("localhost","","","");
}
return $_conn;
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()
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,
];
}
?>
+54 -22
View File
@@ -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 } ?>