2010-05-15 116 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; 
}