Jump to content

[SOLVED] Php/MySql Rankings


zackcez

Recommended Posts

I know that this will not work...but here's what I have:

<?php
$user = Zack;
function getRank($user){
$query = "SELECT * FROM user ORDER BY score";
$rank = 0;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$rank++;
$user_rank = "$rank WHERE username='$user'";
}
}
?>

What I'm trying to do is make it find the rank for every user but store the one for a certain user (Zack).

 

If anyone knows how I can make that work or a better way to do it, please post here :P

Link to comment
https://forums.phpfreaks.com/topic/115745-solved-phpmysql-rankings/
Share on other sites

Well, I'm not sure if this would work but I was messing around and I came up with this:

<?php
$user = Zack;
function getRank($user){
$query = "SELECT * FROM user";
$rank = 0;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$rank++;
$test_rank[" . $row['username'] . "] = $rank;
echo $test_rank[$user];
}
}
getRank($user);
?>
Anyone Know if that'd work?

function getRank ($user)
{
    $sql = "SELECT COUNT(*)+1 as rank 
            FROM user
            WHERE score > (SELECT score FROM user WHERE username = '$user')";
    $res = mysql_query($sql);
    return mysql_result($res,0);        
}

$rank = getRank('Zack');
echo $rank;

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.