Davey
09-06-2004, 04:40 PM
I know why this query returns 'Array', but I can't figure out how to get around it.
function getRank($rating)
{
global $dbase;
if ($rating < 0)
{
$rating = 0;
}
$max_rank_query = mysql_query("SELECT * FROM `chronos_ranks` ORDER BY `rating` DESC LIMIT 1",$dbase);
$max_rank = mysql_fetch_array($max_rank_query);
$max_rank = $max_rank['id'];
$rank_query = mysql_query("SELECT * FROM `chronos_ranks` WHERE `rating`<='" . $rating . "' ORDER BY `rating` DESC",$dbase);
$rank = mysql_fetch_array($rank_query);
if ($max_rank == $rank['id'])
{
$max_rating_query = mysql_query("SELECT * FROM `chronos_players` ORDER BY `rating` DESC LIMIT 1",$dbase);
$max_rating = mysql_fetch_array($max_rating_query);
$max_rating = $max_rating['rating'];
if ($max_rating > $rating)
{
$rank = mysql_fetch_array($rank_query);
}
}
return $rank;
}
I'm trying to return the rank which the rating fits into.
I tried:
$getrank = mysql_query("SELECT * FROM `chronos_ranks` WHERE `rating`<='" . $getuser['rating'] . "'",$dbase);
$getrank = mysql_fetch_array($getrank);
Trouble was, it returned "New Recruit ( 0 )" when I wanted it to return "Sergeant Major ( 7 )". When I replaced <= with >=, it returned "Second Lieutenant ( 8 )", which was 1 rank above what I needed.
I looked at the code of Babstats, and eventually found the function which determines the rank from the rating (which I copied the syntax of, see above function getRank() ).
function GetRank($rating) {
global $players_table, $ranks_table;
if($rating < 0) $rating = 0;
$max_rank_query = DBQuery("SELECT * FROM $ranks_table ORDER BY rating DESC LIMIT 1");
$max_rank = DBFetchArray($max_rank_query);
$max_rank = $max_rank["id"];
$rank_query = DBQuery("SELECT * FROM $ranks_table WHERE rating<='$rating' ORDER BY rating DESC");
$rank = DBFetchArray($rank_query);
if($max_rank == $rank["id"]) {
$max_rating_query = DBQuery("SELECT * FROM $players_table ORDER BY rating DESC LIMIT 1");
$max_rating = DBFetchArray($max_rating_query);
$max_rating = $max_rating["rating"];
if($max_rating > $rating) {
$rank = DBFetchArray($rank_query);
}
}
return $rank;
}
/*I don't believe I am breaking copyright laws, since I am
only posting a function not the whole file.*/
I would like to keep the function I copied, but is there any way to fix it, so that it returns the rank it's supposed to rather than 'Array' ?
Thanks for any help.
function getRank($rating)
{
global $dbase;
if ($rating < 0)
{
$rating = 0;
}
$max_rank_query = mysql_query("SELECT * FROM `chronos_ranks` ORDER BY `rating` DESC LIMIT 1",$dbase);
$max_rank = mysql_fetch_array($max_rank_query);
$max_rank = $max_rank['id'];
$rank_query = mysql_query("SELECT * FROM `chronos_ranks` WHERE `rating`<='" . $rating . "' ORDER BY `rating` DESC",$dbase);
$rank = mysql_fetch_array($rank_query);
if ($max_rank == $rank['id'])
{
$max_rating_query = mysql_query("SELECT * FROM `chronos_players` ORDER BY `rating` DESC LIMIT 1",$dbase);
$max_rating = mysql_fetch_array($max_rating_query);
$max_rating = $max_rating['rating'];
if ($max_rating > $rating)
{
$rank = mysql_fetch_array($rank_query);
}
}
return $rank;
}
I'm trying to return the rank which the rating fits into.
I tried:
$getrank = mysql_query("SELECT * FROM `chronos_ranks` WHERE `rating`<='" . $getuser['rating'] . "'",$dbase);
$getrank = mysql_fetch_array($getrank);
Trouble was, it returned "New Recruit ( 0 )" when I wanted it to return "Sergeant Major ( 7 )". When I replaced <= with >=, it returned "Second Lieutenant ( 8 )", which was 1 rank above what I needed.
I looked at the code of Babstats, and eventually found the function which determines the rank from the rating (which I copied the syntax of, see above function getRank() ).
function GetRank($rating) {
global $players_table, $ranks_table;
if($rating < 0) $rating = 0;
$max_rank_query = DBQuery("SELECT * FROM $ranks_table ORDER BY rating DESC LIMIT 1");
$max_rank = DBFetchArray($max_rank_query);
$max_rank = $max_rank["id"];
$rank_query = DBQuery("SELECT * FROM $ranks_table WHERE rating<='$rating' ORDER BY rating DESC");
$rank = DBFetchArray($rank_query);
if($max_rank == $rank["id"]) {
$max_rating_query = DBQuery("SELECT * FROM $players_table ORDER BY rating DESC LIMIT 1");
$max_rating = DBFetchArray($max_rating_query);
$max_rating = $max_rating["rating"];
if($max_rating > $rating) {
$rank = DBFetchArray($rank_query);
}
}
return $rank;
}
/*I don't believe I am breaking copyright laws, since I am
only posting a function not the whole file.*/
I would like to keep the function I copied, but is there any way to fix it, so that it returns the rank it's supposed to rather than 'Array' ?
Thanks for any help.