5

我正在编写一个带有评论的新闻更新系统。我有三张桌子。

  1. news_tbl
  2. users_tbl(新闻文章的作者)
  3. 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 ?>&nbsp;&nbsp;|&nbsp;&nbsp;<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
}
?> 
4

2 回答 2

1

函数 GROUP_CONCAT 对生成的连接字符串的长度有限制。您很可能通过尝试连接 HTML 或可能很长的注释来快速达到此限制。

我发现最好从 news_tbl 运行第一个查询,并在 PHP 中使用来自 news_tbl 的所有唯一 ID 构建一个数组。接下来,运行第二个查询以获取新闻记录的所有评论。例如:

$sqlquery = "SELECT * FROM news_tbl LIMIT 10";

$result = $conn->query($sqlquery);
$newsIds = array();
foreach($result as $row) {
    $newsIds[] = $row['id'];
}
$sql = "SELECT * FROM comments_tbl WHERE news_id IN (".implode(', ', $newsIds).")";
$result2 = $conn->query($sql);

// process each comment individually from $result2
于 2013-01-12T06:30:05.383 回答
0

如果我正确理解了您的问题,您可以将 group_concat(comments_tbl.comments_id) 和您当前的 group_concat() 内容作为一列,例如:

GROUP_CONCAT(CONCAT(comments_tbl.comments_id,'|||','<hr><h6>Navn: ', comments_tbl.name,'</h6>','<p>',comments_tbl.comment, '</p>')) as something

然后在 php

list($comment_id,$comment_content) = explode('|||',$row['something']);
于 2012-12-19T18:01:53.567 回答