Jump to content


Photo

Get position in a while loop


  • Please log in to reply
4 replies to this topic

#1 Millar

Millar
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts
  • LocationSouthampton, UK.

Posted 08 August 2006 - 10:18 AM

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

$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>";

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

Thanks.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 08 August 2006 - 02:25 PM

You could do this in MySQL, but it sounds like a PHP counter variable would be more useful.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 Millar

Millar
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts
  • LocationSouthampton, UK.

Posted 08 August 2006 - 05:29 PM

How would I do it in PHP?

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 August 2006 - 05:57 PM

To create a counter variables use this:
// 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++;
}
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

#5 Millar

Millar
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts
  • LocationSouthampton, UK.

Posted 08 August 2006 - 11:26 PM

Kool, thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users