"Top 10" from database
Moderator: General Moderators
"Top 10" from database
If I have a column "score" and I want to display the top 10 accounts with the highest integer values in "score", how would I go about doing that?
Ok, here's the best that I can code up for now:
(for top 100 scorers)
Any suggestions?
(for top 100 scorers)
Code: Select all
<?
for($i = 0; $i < 100; $i++) {
$sql="SELECT * FROM `accounts` WHERE `score` > 0 LIMIT 1 ORDER BY `score`";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$username = $row["name"];
$score = $row["score"];
echo "Rank $i: $username with a high score of $score";
}
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
SELECT * FROM `accounts` WHERE `score` > 0 ORDER BY `score` DESC LIMIT 100Code: Select all
?php
for($i = 0; $i < 100; $i++) {
$sql="SELECT * FROM `accounts` WHERE `points` > 0 ORDER BY `points` DESC LIMIT 100";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$username = $row["name"];
$score = $row["points"];
echo "<p>Rank $i: $username with a high score of $score";
}
?>Any ideas?
That's because your query is inside the loop...
Code: Select all
<?php
$sql="SELECT * FROM `accounts` WHERE `points` > 0 ORDER BY `points` DESC LIMIT 100";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$username = $row["name"];
$score = $row["points"];
echo "<p>Rank $i: $username with a high score of $score";
}
?>