我构建排名页面,并希望为每行添加数字我能那样做吗?
我想数一下那里的桌子。我是PHP新手。
// Showing Ranking list
$sql = "SELECT * FROM `userpoint`,`users` WHERE `users`.ID= `userpoint`.uID ORDER BY upoint DESC LIMIT 0, 30";
//Get Username by text
$username= "SELECT * FROM `users`
INNER JOIN `userpoint` on userpoint.uid = users.ID ";
$link_address= "http://***.co.il/profile/?username=";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='grid_3 grid_5' style='width: 400px; position: absolute; margin-right: 600px;'>";
echo "<tr>";
echo "<th style='text-align: right;'>#</th>";
echo "<th style='text-align: right;'>Name</th>";
echo "<th style='text-align: right;'>Points</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
$userlogin = $row[user_login];
echo "<tr>";
echo "<td>#</td>";
echo "<td> <a href='$link_address$userlogin'> " . $row['display_name'] . "</td></a>";
echo "<td>" . $row['upoint'] . "</td>";
echo "</tr>";
}
echo "</table>";
您可以创建一个计数器变量$i
,并在while()
循环之前设置它。
然后将$i
放在循环中需要数字(计数器)值的任何位置。
$i++
在每次迭代中递增计数器。阅读手册中的更多增量值:http://php.net/manual/en/language.operators.increment.php
$i = 1; // set the counter's start point
while ($row = mysqli_fetch_array($result)) {
$userlogin = $row[user_login];
echo "<tr>";
echo "<td>#" . $i . "</td>";
echo "<td> <a href='$link_address$userlogin'> " . $row['display_name'] . "</td></a>";
echo "<td>" . $row['upoint'] . "</td>";
echo "</tr>";
$i++; // here the counter gets increased by 1, so the following iteration it will be $i + 1
}
现在,在循环的每次迭代中,您的页面将显示以下(伪)结果:
#1
#2
#3
#4
etc...
无论$i
在哪里。