OK...im building this stats tracking for a video game where I input final results into the DB and it generates some output...i have most of it working...here is a Breif descrption of my problem

PHP Code:
$sortby "margin_permap ASC";
$sql="SELECT * FROM $playerstable ORDER BY $sortby";
$result=mysql_query($sql,$db) or die (mysql_error());
$num mysql_num_rows($result);
$cur 1;

while (
$num >= $cur) {
$row mysql_fetch_array($result);
$name $row["name"];
$squad  $row["squad"];
$scores $row["scores"];
$kills $row["kills"];
$deaths $row["deaths"];
$maps_played $row["maps_played"];
$margin_permap $row["margin_permap"];    
$margin $scores-$deaths;
$ratio $kills/$deaths;
$deaths_permap $deaths/$maps_played;
$scores_permap $scores/$maps_played;
$kills_permap $kills/$maps_played;
$margin_permap $margin/$maps_played;

I Can Not get the Output to show $MARGIN_PERMAP in the correct order best to worst..I can change the $sortby to kills and it will output correctly so that kinda tells me the Calculation Im making in this cant do the Fuction twice??

by looking at the above info I supplied can you understand what Im trying to do...here is a Link of the output and notice how the LAST COLUMN doesnt sortby best to worst score..ADMIN should be listed in 1st place with 18.17


My Code may be CRUDE but I am TOTAL Newbie..actually just finished reading my 1st book...lol...but I love learning from everyone...any help would be appreciated...i think i know what problems are..just dont know how to fix it