开发者

add a number in while loop

     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>&l开发者_高级运维t;?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";
                }
            ?>

I am looking for a way to add a number to each comment. So 1, 2, 3, 4, etc in DESC order. I can't think how I can do this?


I've added a few lines to your code which should accomplish what you want.

 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";
                }
            ?>


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

$counter -= 1;

echo $counter;
}


Add an increment $no++ to your loop and display it in my table <td><?= $no ?><?td>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜