Jump to content

Get position in a while loop


Millar

Recommended Posts

I have this piece of code so far to order by the power...

[code]$result = mysql_query("SELECT * FROM reg ORDER BY power DESC") ; 

print "<table>";
print "<tr> <th>Name</th></tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
print "<tr><td><a href='index.php?act=stats&id=" . $row['id'] . "'>";
print $row['user'];
print "</a></td></tr>";
}

print "</table>";[/code]

But, how can I get the position in the array the particular name is, so it is displayed like a ranking by power.

Thanks.
Link to comment
https://forums.phpfreaks.com/topic/16875-get-position-in-a-while-loop/
Share on other sites

To create a counter variables use this:
[code]// prepare out counter variable:
$i = 1;
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
print "<tr><td>{$i} - <a href='index.php?act=stats&id=" . $row['id'] . "'>";
print $row['user'];
print "</a></td></tr>";

        // now we increment the counter var by 1
        $i++;
} [/code]
That produce this:
1 - [name here]
2 - [name here]
3 - [name here]
etc
Obiviosly name here will be replaced with the name in the database

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.