Millar Posted August 8, 2006 Share Posted August 8, 2006 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 getwhile($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 More sharing options...
fenway Posted August 8, 2006 Share Posted August 8, 2006 You could do this in MySQL, but it sounds like a PHP counter variable would be more useful. Link to comment https://forums.phpfreaks.com/topic/16875-get-position-in-a-while-loop/#findComment-71163 Share on other sites More sharing options...
Millar Posted August 8, 2006 Author Share Posted August 8, 2006 How would I do it in PHP? Link to comment https://forums.phpfreaks.com/topic/16875-get-position-in-a-while-loop/#findComment-71258 Share on other sites More sharing options...
wildteen88 Posted August 8, 2006 Share Posted August 8, 2006 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]etcObiviosly name here will be replaced with the name in the database Link to comment https://forums.phpfreaks.com/topic/16875-get-position-in-a-while-loop/#findComment-71274 Share on other sites More sharing options...
Millar Posted August 8, 2006 Author Share Posted August 8, 2006 Kool, thanks. Link to comment https://forums.phpfreaks.com/topic/16875-get-position-in-a-while-loop/#findComment-71500 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.