On implémente un petit système de boutons "J'aime" / "Je n'aime pas" sur nos articles en PHP ! Bien sûr, il est tout à fait possible d'utiliser ce système sur d'autres types de posts (images, commentaires, etc.) pour laisser aux utilisateurs la possibilité d'exprimer leur avis ☺
- article.php
<?php
$bdd = new PDO("mysql:host=127.0.0.1;dbname=articles;charset=utf8", "root", "");
if(isset($_GET['id']) AND !empty($_GET['id'])) {
$get_id = htmlspecialchars($_GET['id']);
$article = $bdd->prepare('SELECT * FROM articles WHERE id = ?');
$article->execute(array($get_id));
if($article->rowCount() == 1) {
$article = $article->fetch();
$id = $article['id'];
$titre = $article['titre'];
$contenu = $article['contenu'];
$likes = $bdd->prepare('SELECT id FROM likes WHERE id_article = ?');
$likes->execute(array($id));
$likes = $likes->rowCount();
$dislikes = $bdd->prepare('SELECT id FROM dislikes WHERE id_article = ?');
$dislikes->execute(array($id));
$dislikes = $dislikes->rowCount();
} else {
die('Cet article n\'existe pas !');
}
} else {
die('Erreur');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Accueil</title>
<meta charset="utf-8">
</head>
<body>
<img src="miniatures/<?= $id ?>.jpg" width="400" />
<h1><?= $titre ?></h1>
<p><?= $contenu ?></p>
<a href="php/action.php?t=1&id=<?= $id ?>">J'aime</a> (<?= $likes ?>)
<br />
<a href="php/action.php?t=2&id=<?= $id ?>">Je n'aime pas</a> (<?= $dislikes ?>)
</body>
</html>
- php/action.php
<?php
$bdd = new PDO("mysql:host=127.0.0.1;dbname=articles;charset=utf8", "root", "");
if(isset($_GET['t'],$_GET['id']) AND !empty($_GET['t']) AND !empty($_GET['id'])) {
$getid = (int) $_GET['id'];
$gett = (int) $_GET['t'];
$sessionid = 5;
$check = $bdd->prepare('SELECT id FROM articles WHERE id = ?');
$check->execute(array($getid));
if($check->rowCount() == 1) {
if($gett == 1) {
$check_like = $bdd->prepare('SELECT id FROM likes WHERE id_article = ? AND id_membre = ?');
$check_like->execute(array($getid,$sessionid));
$del = $bdd->prepare('DELETE FROM dislikes WHERE id_article = ? AND id_membre = ?');
$del->execute(array($getid,$sessionid));
if($check_like->rowCount() == 1) {
$del = $bdd->prepare('DELETE FROM likes WHERE id_article = ? AND id_membre = ?');
$del->execute(array($getid,$sessionid));
} else {
$ins = $bdd->prepare('INSERT INTO likes (id_article, id_membre) VALUES (?, ?)');
$ins->execute(array($getid, $sessionid));
}
} elseif($gett == 2) {
$check_like = $bdd->prepare('SELECT id FROM dislikes WHERE id_article = ? AND id_membre = ?');
$check_like->execute(array($getid,$sessionid));
$del = $bdd->prepare('DELETE FROM likes WHERE id_article = ? AND id_membre = ?');
$del->execute(array($getid,$sessionid));
if($check_like->rowCount() == 1) {
$del = $bdd->prepare('DELETE FROM dislikes WHERE id_article = ? AND id_membre = ?');
$del->execute(array($getid,$sessionid));
} else {
$ins = $bdd->prepare('INSERT INTO dislikes (id_article, id_membre) VALUES (?, ?)');
$ins->execute(array($getid, $sessionid));
}
}
header('Location: http://127.0.0.1/Tutos_PHP/Articles/article.php?id='.$getid);
} else {
exit('Erreur fatale. <a href="http://127.0.0.1/Tutos_PHP/Articles/">Revenir à l\'accueil</a>');
}
} else {
exit('Erreur fatale. <a href="http://127.0.0.1/Tutos_PHP/Articles/">Revenir à l\'accueil</a>');
}
Votre commentaire