PDA

View Full Version : PHP Ranking?



Moh
15-07-2008, 05:58 AM
I have researched on how to do this, but I could only find the ranks for a full list.
Does any one know how I would get thank rank of a specific user?

So in the database, it looks like:

username -- points
------------------
jack -- 50
ryan -- 65
skier -- 48
david -- 67

So if you was logged in as skier, it would say "Your current rank is 4"
and for ryan, it would say "Your current rank is 2" etc..

+Rep for any help :)

Lemonade
15-07-2008, 06:04 AM
$1 = mysql_query("select * from TABLE where `USER`='session'");
$2 = mysql_fetch_array($1);
echo("<b>" . $2[username] . "</b> - " . $2[points] . "");

Im sorta new to php.. Sorry if this is the wrong thing but is that the sort of thing you mean?

Blinger1
15-07-2008, 06:52 AM
Wouldn't it be easier if you had the list in descending order from the most points to the least?

Lemonade
15-07-2008, 12:25 PM
ops, I understand what you mean now,
I didn't read the post properly...

Trigs
15-07-2008, 01:40 PM
I think you would order them by interger then pull them by id?

Moh
15-07-2008, 01:59 PM
$1 = mysql_query("select * from TABLE where `USER`='session'");
$2 = mysql_fetch_array($1);
echo("<b>" . $2[username] . "</b> - " . $2[points] . "");

Im sorta new to php.. Sorry if this is the wrong thing but is that the sort of thing you mean?


Wouldn't it be easier if you had the list in descending order from the most points to the least?


I think you would order them by interger then pull them by id?
What I want it to do is order them all by points DESC and then giving me the position of the entry out the full list.

Trigs
15-07-2008, 08:26 PM
Oh,
("SELECT * FROM changeme WHERE changeme ORDER BY rank desc");

Invent
15-07-2008, 08:47 PM
What I want it to do is order them all by points DESC and then giving me the position of the entry out the full list.



$query = mysql_query( "SELECT * FROM `changeme` ORDER BY `points` DESC" );

$i = 1;

while( $f = mysql_fetch_array( $query ) )
{

echo "[". $i . "] ". $f["username"] . " - ". $f["points"] ." points<br />";

$i++;

}
That'd order them like:

[1] David - 67 points
[2] Ryan - 65 Points
[3] Jack - 50 points
[4] Skier - 48 points

I think that's what you want?

Hypertext
15-07-2008, 08:56 PM
<?php

$i = 0;

$q = mysql_query("select * from ranks where user='jack'");
$f = mysql_fetch_array($q);

$wq = mysql_query("select * from ranks where user not like 'jack'");
$wf = mysql_fetch_array($wq);
$top = 1;

foreach($wq as $val => $k) {
if($wq['points'] > $f['points']) {
++$top;
}

echo "Your rank is " . $top;

?>


untested will work with tweaks - probably a better way

Moh
15-07-2008, 09:21 PM
Oops, forgot to post that I have done it :P

I used the way simon did it, but when it got you your rank, it stopped and set a variable as $x. Then it just returned the variable :P

Want to hide these adverts? Register an account for free!