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
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.