tags:

views:

48

answers:

1

The idea is to run a sort of ranking MySQL command such as:

$sql = mysql_query("SELECT f_score FROM ".TBL_FACTIONS." ASC");

EDIT: I do need to add a WHERE f_id = $id in here too.

The numeric value in my MySQL database table in the *f_score* column should determine the rank. f_score contains only numeric values, from 0 to anything, user actions add and subtract from the score.

then use the $sql to loop through an array and "rank" each array element. I think it should assign a number to a specific variable so I can echo the number out on the webpage.

E.g. Your rank is: 01

I'm looking for something lightweight, but if its not possible to do this win minimum server usages, I'll have to shove it into a cron job or something which isn't really ideal. I was thinking along the lines of array and loops or something similar?

Any help would be greatly appreciated!

-Callum

+1  A: 

This code:

$result = mysql_query("SELECT f_name FROM ".TBL_FACTIONS." ORDER BY f_score DESC");
$i = 1;

while($row = mysql_fetch_assoc($result))
{
    echo "{$row['f_name']} & Rank: {$i}<br>";
    $i++;
}

Will output:

Bla bla & Rank: 1
Foo bar & Rank: 2
Etc.

The SQL for the rank of a specific record is a bit complicated. You could try the following:

// E.g. rank for f_id = 123
$id = 123;

$sql = "
SELECT `a`.`rank` + `b`.`rank` AS `rank`
FROM (SELECT COUNT(*)+1 AS `rank`
    FROM ".TBL_FACTIONS."
    WHERE `f_score` > (SELECT `f_score`
        FROM ".TBL_FACTIONS."
        WHERE `f_id` = {$id})) AS `a`
STRAIGHT_JOIN (SELECT COUNT(*) AS `rank`
    FROM ".TBL_FACTIONS."
    WHERE `f_score` = (SELECT `f_score`
        FROM ".TBL_FACTIONS."
        WHERE `f_id` = {$id}) AND `f_id` < {$id}) AS `b`
";

$result = mysql_query($sql);
$row = mysql_fetch_row($result);
echo "{$id} is ranked {$row[0]}";

I found the query here and it makes sense, but please test thoroughly if the results you get are what you expect it to be.

Alternatively, if your recordset is not too big, you could just loop through the results and break out of the loop when you get to the ID you are looking for:

$id = 123;
$result = mysql_query("SELECT f_id FROM ".TBL_FACTIONS." ORDER BY f_score DESC");
$i = 1;

while($row = mysql_fetch_assoc($result))
{
    if($row['f_id'] == $id)
    {
        echo "{$row['f_id']} is ranked {$i}<br>";
        break;
    }
    $i++;
}
captaintokyo
yeah, your right actually. Bummer, i forgot to think about that :/Erm, I need to add a WHERE f_id = $id in there to display it correctly for the selected faction.
Callum Johnson
also, knowing how to show who is ranked 1,2,3 would be really useful for another part of the webpage actually
Callum Johnson
I still don't understand what you are trying to do here. Do you want something like: `Faction ID 23 - your rank is: 1`. What exactly do you want to show on the webpage?
captaintokyo
I updated my answer. Is this what you are after?
captaintokyo
Callum Johnson
If i could get a way to echo faction id: ranked 1. Where the ranked value assigned to this id from the f_score value. I could take it from there?
Callum Johnson
captaintokyo
what you have posted is what I was looking for. Question answered. Thank you for your time.I do have one question, could I modify this code to only display one specific id's rank and not echo the others?
Callum Johnson
By the way, shouldn't the faction with the highest score be ranked 1? If you use `ORDER BY f_score ASC`, the faction with the lowest score will be ranked 1.
captaintokyo
Thank you for the heads up! Silly mistake of mine, i'll change it to DESC then :)
Callum Johnson
"I do have one question, could I modify this code to only display one specific id's rank and not echo the others?"
Callum Johnson
I added the modification you are looking for
captaintokyo
@Callum Johnson: The COUNT subselect has a flaw - if there are duplicates, they will have the same rank/position value. IE: If two are tied for second place, the rank will be "2" for both of them.
OMG Ponies
trying the first explanation works perfectly if f_id is simply a numeric value. It throws a boolean error if a aplha numeric is used as f_id, any ideas on how to resolve this?
Callum Johnson
@OMG Ponies: What is the best way to modify it to then rank by alphabetical order if the scores and subsequently the ranks are equal?
Callum Johnson
just add single quotes around `{$id}` if it's not an integer: `'{$id}'`
captaintokyo