2014-02-12 47 views
0

我試圖在我的網站上開發一個用戶配置文件系統,其中包含用戶創建的前3個帖子。我可以讓它選擇前面的3條記錄,但它只會顯示其中的一條。我只是在愚蠢,因爲我想在凌晨2點進行編碼?PHP - 在表格中顯示最後3個SQL行(不工作)

<?php      
    $q = "SELECT * FROM blog_cmt WHERE uid=".$profile_uid." ORDER BY id DESC LIMIT 3"; 
    $r = $db->query($q);         
    $c = $r->num_rows;        
?> 

<table class="table"> 
    <?php 
    while($post = $r->fetch_assoc()) { 
     $pid = $post['pid']; 
     $q = "SELECT * FROM blog WHERE pid=".$pid; 
    $r = $db->query($q); 
    $blog = $r->fetch_assoc(); 
    $title = $blog['title']; 
    $date = timeSince(strtotime($post['date'])); 
    ?> 
    <tr> 
     <td> 
      <a href="http://www.my_domain.co.uk/thread.php?id=<?php echo $pid; ?>">Commented on <?php echo $title; ?></a> 
     </td> 
     <td> 
      <?php echo $date; ?> ago 
     </td> 
    </tr> 
    <?php 
    } 
    ?> 
</table> 
+1

你一直覆蓋$ R以單篇文章ID:$ R = $ DB->查詢($ Q); – Trent

+0

那麼每次使用不同的變量名? – Zer0HD2

+1

當然 - 你在一個「while」循環 - 所以如果$ r最初有3條記錄,那麼你將$ r重寫爲一條記錄,下一個循環就已經使用了$ r – Trent

回答

0
<?php 

$profile_post_qry = "SELECT * FROM blog_cmt WHERE uid=".$profile_uid." ORDER BY id DESC LIMIT 3"; 
$profile_posts = $db->query($profile_post_qry); 

$profile_post_count = $profile_posts->num_rows; 

?> 

<table class="table"> 

<?php 

while($post = $profile_posts->fetch_assoc()) { 

$pid = $post['pid']; 
$blog_qry = "SELECT * FROM blog WHERE pid=".$pid; 
$blog_info = $db->query($blog_qry); 
$blog = $blog_info->fetch_assoc(); 
$title = $blog['title']; 
$date = timeSince(strtotime($post['date'])); 

?> 

<tr> 
<td> 
    <a href="http://www.my_domain.co.uk/thread.php?id=<?php echo $pid; ?>">Commented on <?php echo $title; ?></a> 
</td> 
<td> 
    <?php echo $date; ?> ago 
</td> 
</tr> 

<?php