LEFT JOIN ON

Par aStunner, il y a 11 ans


Bonsoir
j'ai 2 tables : commentaires et articles
articles contient (id, title, author, content, date_post) et
commenaitres contient (id, title, author, content, date_post, article_id)

d'après ma requête je voudrais lié les commentaires à un unique article (si mon article_id vaut 1 alors tout les commentaires seront pour l'article avec l'id 1)

j'ai fais un left join pour sa, voici comment est conçu mon systeme :

index.php :

<?php require 'app/includes.php'; $title = "Les articles"; require 'partials/header.php'; // Récupération des Articles $req = $db->prepare("SELECT * FROM articles ORDER BY date_post DESC LIMIT 0,10"); $req->execute(); $data = $req->fetchAll(); ?> <?php if(array_key_exists('success', $_SESSION)): ?> <div class="alert alert-success"> <?= implode('<p>', $_SESSION['success']); ?> </div> <?php unset($_SESSION['success']); endif; ?> <?php foreach($data as $articles): ?> <div class="panel panel-default"> <div class="panel-heading"> <h3> <?php $url = "articles/".$articles['slug']."-".$articles['id']; echo "<a href=$url>".$articles['title']."</a>"; ?> </h3> <p> <i class="fa fa-clock-o"></i> <?= htmlspecialchars($articles['date_post']); ?> - <i class="fa fa-user"></i> <?= htmlspecialchars($articles['author']); ?> </p> </div> </div> <?php endforeach; ?> <?php require 'app/debug.php'; require 'partials/footer.php'; ?>

articles.php :

<?php require 'app/includes.php'; require 'app/classes/form.php'; $title = "Bievenue sur NcNetwork !"; require 'partials/header.php'; // Récupération de l'article $id = $_GET['id']; $req = $db->prepare("SELECT * FROM articles WHERE id = :id"); $req->bindParam(":id", $id); $req->execute(); $data = $req->fetchAll(); ?> <!-- L'article !--> <?php foreach($data as $article): ?> <div class="page-header"> <h3> <?= htmlspecialchars($article['title']); ?> </h3> <p> Cet article a été posté le <i class="fa fa-calendar"></i> <?= htmlspecialchars($article['date_post']); ?> et écrit par <i class="fa fa-user"></i> <?= htmlspecialchars($article['author']); ?> </p> </div> <p> <?= htmlspecialchars($article['content']); ?> </p> <?php endforeach; ?> <!-- Les commentaires !--> <div class="page-header"> <h3> <i class="fa fa-comments"></i> Les commentaires </h3> </div> <?php // Récupération des commentaires $req = $db->prepare("SELECT commentaires.id, commentaires.title, commentaires.author, commentaires.date_post, commentaires.article_id, articles.id, commentaires.content FROM commentaires LEFT JOIN articles ON commentaires.article_id = $id"); $req->execute(); $dataComm = $req->fetchAll(); foreach($dataComm as $commentaires): ?> <div class="panel panel-default"> <div class="panel-heading"> <h3> <?= htmlspecialchars($commentaires['title']); ?> </h3> <p> Ce commentaire a été posté le <i class="fa fa-calendar"></i> <?= htmlspecialchars($commentaires['date_post']); ?> et écrit par <i class="fa fa-user"></i> <?= htmlspecialchars($commentaires['author']); ?> </p> </div> <div class="panel-body"> <p> <?= htmlspecialchars($commentaires['content']); ?> </p> </div> </div> <?php endforeach; ?> <?php if(isset($_SESSION['Auth']['id'])): ?> <?= Form::start("post", "app/posts/commentaires.php"); ?> <?= Form::end(); ?> <?php endif; ?> <?php if(!isset($_SESSION['Auth']['id'])): ?> <div class="alert alert-info"> <p> Vous devez être connecté pour pouvoir posté un commentaire ! </p> </div> <?php endif; ?> <?php require 'app/debug.php'; require 'partials/footer.php'; ?>

1 réponse

connected, il y a 11 ans

Bonsoir,

Ta deuxième requête dans article.php n'a pas besoin de LEFT JOIN, tu dois juste faire une condition sur article_id = id (que tu souhaite)