2012-12-15 89 views
5

我正在編寫帶有評論的新聞更新系統。我有三張桌子。如何從GROUP_CONCAT中刪除單個行?

  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 
} 
?> 
+3

我不會在這裏使用一個組,而只是獲取單個記錄。但是,如果你想這樣:爲什麼不做一個單獨的'GROUP_CONCAT(comments_tbl.comments_id)'? – Wrikken

+0

那麼,我該怎麼做呢,沒有GROUP_CONCAT?我嘗試了不同的JOINS,但是如果沒有GROUP_CONCAT,我就無法工作,所以我可以顯示每條新聞和附加的所有評論,我只會收到一條評論,或者重複,而且我還沒有獲得單獨的ID評論 - 刪除它們。 – user1906437

回答

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']); 
1

功能GROUP_CONCAT對生成的連接字符串可以有多長時間有限制。通過嘗試連接HTML或可能長的評論,您很可能會很快達到此限制。

我發現運行news_tbl中的第一個查詢更好,並使用來自news_tbl的所有唯一ID在PHP中構建一個數組。接下來,運行第二個查詢以獲取新聞記錄的所有評論。例如:

$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 
相關問題