2013-10-13 40 views
1

所以我有一個腳本,顯示所有與主題信息相關的主題,我想知道是否有人可以幫我做到這一點,所以每當有人評論它或如果該帖子被編輯它將它撞到查詢的第一行echod?製作查詢顯示最新更新

function getReplies($id){ 
     $q = @mysql_query("SELECT reply_content, reply_date, reply_by FROM replies WHERE reply_id='$id'"); 
     if(!$q){ 
      echo 'Error: '.mysql_error(); 
     } 

     $res = mysql_fetch_array($q); 
     $q2 = @mysql_query("SELECT `topic_subject` FROM `topics` WHERE `topic_id`='$id'"); 
     if(!$q2){ 
      echo 'Error: '.mysql_error(); 
     } 

     $res2 = mysql_fetch_array($q2); 
     if($_SESSION['id'] == $res['reply_by']){ 
      echo '<div class="panel panel-default"> 
    <div class="panel-heading"><b>'.$res2['topic_subject'].'</b></div> 
    <div class="panel-body"> 
    '.nl2br($res['reply_content']).' 
    </div> 
       <div class="panel-footer"> 
        Posted By <strong><a href="../public.php?id='.$res["reply_by"].'">'.getOwner($res['reply_by']).'</stong></a> 
        on <strong>'.$res['reply_date'].'</strong><br /> 
        <a href="edit.php?id='.$id.'">Edit</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="delete.php?id='.$id.'">Delete</a> 
       </div> 
</div> 
      </div>'; 
     } else { 
      echo'<div class="panel panel-default"> 
    <div class="panel-heading"><b>'.$res2['topic_subject'].'</b></div> 
    <div class="panel-body"> 
    '.nl2br($res['reply_content']).' 
    </div> 
       <div class="panel-footer"> 
        Posted By <strong><a href="../public.php?id='.$res["reply_by"].'">'.getOwner($res['reply_by']).'</stong></a> 
        on <strong>'.$res['reply_date'].'</strong><br /> 
       </div> 
</div> 
      </div>'; 
     } 

    } 

評論: Comments structure in DB

帖子: Posts structure in DB

主題: Topics structure in DB

+0

如果你的意思了,如果沒有頁面動態發生清爽,那麼你就需要使用JavaScript來XHR(AJAX )來自服務器的數據並相應地更新DOM。 –

+0

是的,但現在我想從簡單的開始,當有人回覆它時將其置於頂端。 –

+0

看着你的代碼,並試圖爲你優化它,但我無法完全看到你爲表格發佈的圖片。你能解釋一下你在做什麼嗎?例如,$ _SESSION ['id']是什麼意思?另外,你能否將文章中的數據庫關係發佈? –

回答

1

試圖改變自己的第一個查詢,並授予它的ORDER BY,也許?

$q = "SELECT reply_content, reply_date, reply_by 
     FROM  replies 
     WHERE reply_id='$id' 
     ORDER BY reply_date DESC"; 

在我看來,你也使用兩個查詢,你只需要使用兩個查詢。

更新:優化代碼,儘量不重複自己,並加入查詢

function getReplies($id) { 

     $query = "SELECT replies.reply_content, 
         replies.reply_date, 
         replies.reply_by 
         topics.topic_subject 
       FROM  replies, 
         topics 
       WHERE replies.reply_id = topics.topic_id 
        AND replies.reply_id = '$id' " . //<== PS: You should sanitize this $id parameter 
       " ORDER BY replies.reply_date DESC"; 
       // You can replace the replies.reply_date column by whatever you end up with for sorting 

     // Important note - Look into mysqli, mysql is deprecated and too old to be used these days. 
     $q = @mysql_query($query); 

     if(!$q) { 
      echo 'Error: '.mysql_error(); 
     } 

     $res = mysql_fetch_array($q); 

     $output = ' 
     <div class="panel panel-default"> 
      <div class="panel-heading"><b>'.$res['topic_subject'].'</b></div> 
      <div class="panel-body"> 
       '.nl2br($res['reply_content']).' 
      </div> 
      <div class="panel-footer"> 
       Posted By <strong><a href="../public.php?id='.$res["reply_by"].'">'.getOwner($res['reply_by']).'</stong></a> 
       on <strong>'.$res['reply_date'].'</strong><br />'; 

     if($_SESSION['id'] == $res['reply_by']) 
     $output .= '<a href="edit.php?id='.$id.'">Edit</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="delete.php?id='.$id.'">Delete</a>'; 

     $output .= ' 
      </div> 
      </div> 
     </div>'; 

     echo $output; 

    } 
+0

謝謝!雖然如何在出現新評論時做出這項工作?我可以通過數據庫中的評論日期時間字段對其進行排序,並且如果沒有評論通過reply_date DESC排序嗎? –

+0

我想說當有人評論更新reply_date到現在(),但這沒有多大意義。如果我添加了另一個日期時間字段並按此順序排序,並在您將它更新爲now()時進行了更改,會怎麼樣?聽起來不錯? –

+0

是的,你會使用reply_last_update列或其他東西。我會建議,如果這實際上並不是什麼reply_date。稍等一下,你也會嘗試和優化你的代碼。 –