Jump to content

Archived

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

Millar

Get position in a while loop

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.

Share this post


Link to post
Share on other sites
You could do this in MySQL, but it sounds like a PHP counter variable would be more useful.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.