2010-05-15 113 views
0
 if ($num_rows > 0) 
       { 
        while($row=mysql_fetch_assoc($res)) 
        { 
         $fromuser=$row['username']; 
         $comment=$row['comment']; 
         $commentdate=$row['date']; 

         $date=strtotime($commentdate); 
         $final_date=date("g:i a", $date); 
         $final_date2=date("F j Y", $date); 

      ?> 


      <table align="center" width="100%"style='border-top: 1px dotted;'bgcolor="#eeeeee" > 
      <tr><td><?echo "<a href=\"userprofile.php?user=$fromuser\"><b>$fromuser</b></a> commented:\n";?></td></tr> 
      <tr><td><?echo "at $final_date on $final_date2\n";?></td></tr> 
      <tr bgcolor="#ffffff"><td><?echo "$comment\n";?></td></tr> 
      </table><br> 
      <? 
        } 
       } 
       else 
       { 
        echo"There are currently no comments on this user"; 
       } 
      ?> 

我正在尋找一種方法爲每條評論添加一個數字。因此,在DESC順序中有1,2,3,4等。 我想不出我能做到這一點?在while循環中添加數字

+0

添加計數器變量? – 2010-05-15 15:04:05

回答

2

我已經添加了幾行代碼應該完成你想要的。

if ($num_rows > 0) 
       { 
        $number = $num_rows; 
        while($row=mysql_fetch_assoc($res)) 
        { 
         $fromuser=$row['username']; 
         $comment=$row['comment']; 
         $commentdate=$row['date']; 

         $date=strtotime($commentdate); 
         $final_date=date("g:i a", $date); 
         $final_date2=date("F j Y", $date); 

      ?> 


      <table align="center" width="100%"style='border-top: 1px dotted;'bgcolor="#eeeeee" > 
      <tr><td><?echo $number;?><td><?echo "<a href=\"userprofile.php?user=$fromuser\"><b>$fromuser</b></a> commented:\n";?></td></tr> 
      <tr><td><?echo "at $final_date on $final_date2\n";?></td></tr> 
      <tr bgcolor="#ffffff"><td><?echo "$comment\n";?></td></tr> 
      </table><br> 
      <? 
        $number -= 1; 
        } 
       } 
       else 
       { 
        echo"There are currently no comments on this user"; 
       } 
      ?> 
+0

感謝芽,我嘗試了類似的東西,但我無法得到它的工作。 真的很簡單! – sark9012 2010-05-15 17:10:30

0

添加增量$no++你的循環並將其顯示在我的表<td><?= $no ?><?td>

+0

問題以降序的順序要求數字。 – akamike 2010-05-15 15:13:24

+0

是的,我錯過了那部分。我的答案吸了。 – gurun8 2010-05-15 18:02:22

1
$counter = $num_rows; 
    while($row=mysql_fetch_assoc($res)) 
        { 
{ 
// ... your code 

$counter -= 1; 

echo $counter; 
}