我正在编写一个带有评论的新闻更新系统。我有三张桌子。
news_tbl
users_tbl
(新闻文章的作者)comments_tbl
(与外键连接 - news_id)
我尝试了两种方法,有和没有GROUP_CONCAT
。
我试过这个:
SELECT *, COUNT(comments_tbl.comments_id) AS `comments_count`
FROM news_tbl AS n
LEFT JOIN users_tbl AS u
ON n.user = u.username
LEFT JOIN comments_tbl AS c
ON c.news_id = n.news_id
GROUP BY n.news_id;
它有效,但我只能显示第一个书面评论,但是,我想显示所有评论,附加到新闻更新中。
我也使用GROUP_CONCAT
了评论,因此对于每个评论,新闻帖子都不会重复。我不确定这是否是正确的方法,但它有效。
我的问题是,我有一个登录系统,我希望用户能够单独删除评论,每个评论都有一个删除按钮。但我不知道如何单独分隔评论,并从每个评论中获取 id。我已经尝试过使用 PHP explode
,但它对我不起作用。
这是我的代码:
<?php
require_once('connect.inc.php');
$conn = dbConnect('pdo') or die('no connection');
$sqlquery = "SELECT news_tbl.*, users_tbl.*,
GROUP_CONCAT(comments_tbl.comments_id) AS commentsid,
GROUP_CONCAT(CONCAT ('<hr><h6>Navn: ', comments_tbl.name,'</h6>','<p>',comments_tbl.comment, '</p>') SEPARATOR '<br><br>') AS comments,
COUNT(comments_tbl.comments_id) AS comments_count
FROM news_tbl
LEFT JOIN comments_tbl ON comments_tbl.news_id = news_tbl.news_id
LEFT JOIN users_tbl ON news_tbl.user = users_tbl.username
GROUP BY news_tbl.news_id
ORDER BY news_tbl.news_id DESC LIMIT 5";
$result = $conn->query($sqlquery);
foreach($result as $row) {
// AS
$commentsid = $row['commentsid'];
$comments = $row['comments'];
$count = $row['comments_count'];
//users_tbl
$username = $row['username'];
//news_tbl
$newsid = $row['news_id'];
$headline = $row['headline'];
$bodytext = $row['bodytext'];
$picture = $row['picture'];
$date = $row['date'];
//comments_tbl
$commentid = $row['comments_id'];
$name = $row['name'];
$comment = $row['comment'];
?>
<div class="row-fluid">
<div class="news">
<div class="text-left">
<h3 class="pull-top"><?php echo $headline ?></h3>
<div class="row-fluid span12">
<img src="<?php echo 'upload/'.$picture ?>" class="span9 img-news"/>
</div>
<div class="row-fluid">
<div class="span3">
<p class="author"><i><span class="red">Skrevet af: </span> </i><?php echo $username ?></p>
</div>
<div class="span4">
<p><i ><span class="red">Oprettet:</span> </i> <?php echo $date ?></p>
</div>
</div><!--/ End author-row -->
<!--<p class="author"><i><span class="red">Skrevet af: </span> </i><?php echo $username ?> | <i ><span class="red">Oprettet:</span> </i> <?php echo $date ?></p>-->
<p class="push-top-bottom"><?php echo $bodytext ?></p>
</div>
<hr>
<p class="text-left pull-top3"> <b><?php echo $count ?> kommentar</b></p>
<hr class="grey">
<div class="row-fluid pull-top2">
<div class="pull-left span12 text-left well">
<?php echo $comments; ?>
</div>
<!-- Opret kommentar -->
<div class="row-fluid pull-left">
<form action="insert.php?<?php echo 'news_id=' . $newsid; ?>" method="post" class="pull-left leave-comment">
<input type="text" name="name" id="name" class="comment" placeholder="Navn" required/> <br> <br>
<textarea type="text" name="comment" id="comment" class="comment" placeholder="Skriv kommentar" rows="5" required></textarea> <br> <br>
<input type="submit" value="Opret kommentar" class="btn pull-left"/>
</form>
</div>
</div><!--/ End comments-row -->
</div><!--/ End news-container -->
</div><!--/ End news row -->
<?php
if (isset($_SESSION['valid_user'])) {
// onClick slet nyhed med alert box
echo '<div class="row-fluid">';
echo ' <div class="pull-left">';
echo ' <a class="btn btn-danger" onclick="deleteNews('. $newsid.')" style="margin-right:20px;">Slet nyhed</a>';
echo ' <a href=update.php?news_id='.$newsid.'&username='. $username .' class="btn btn-info">Updater nyhed</a>';
echo ' <br><br><br><br>';
echo ' </div>';
echo '</div>';
}
?>
<hr>
<?php
//End foreach
}
?>