Liel Dahan Liel Dahan - 5 months ago 8
PHP Question

Count the rows on table php

I build ranking page and iwant to add number for each row
can ido that ?

want to count the table there
I am new with 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://***";
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>";


You can create a counter variable $i and set it prior to the while() loop.

Then place $i wherever you need your numeric (counter) value in your loop.

$i++ increments the counter on each iteration. Read on more incremental values in the manual:

$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

Now on each iteration of the loop, your page will show the following result:


Wherever $i is present.