I have a list of items in a sql database, and it's ordered by score (+1, -5). How should i do to get the position of like.. ID 53 based on the score and return it? I have no idea on how to do this...
Loop through the database and then put all the stuff in a array then print out using $array[id]; ?
Top50 works that way, not one position.
[editline]03:04PM[/editline]
Dragory helped me.. Thread no more.
Get all items ordered by score, dump it in an array and loop through the array until you find id 53. Then you know the index/position.
[b]AGAIN:[/b] Post your solution so other people with the same problem can look at this and fix their own.
[code]
function getPlace($id){
$query = mysql_query("SELECT * FROM table ORDER BY score DESC");
$pos = 1;
while($row = mysql_fetch_row($query)) {
if($row[0] == (int)$id) break; else $pos++;
}
return $pos;
}
[/code]
See, exactly what I said.
Same as i did too, almost.
Yeah, but i didn't really know how to do it, i'm not an expert :downs:
[QUOTE=Overv;17761966]See, exactly what I said.[/QUOTE]
Except you wanted him to waste memory by putting it all to an array.
[code]
function getPlace($id){
mysql_query("SET @r=0;");
$query = mysql_query("SELECT @r:=@r+1 AS rank, * ORDER BY score DESC HAVING id = $id;");
return mysql_fetch_row($query)[0];
}
[/code]
I'm not too good with PHP, but I know the SQL should work. I'm assuming that you use MySQL. If you use another SQL software, it may have a 'rank()' function.
Sorry, you need to Log In to post a reply to this thread.