Query help

$pack_query = "SELECT * FROM pack";
$pack_query_result = query($pack_query);
while($fishypack = mysqli_fetch_array($pack_query_result)){
$fish_query = "SELECT * FROM fish WHERE fishParticipantName IN (SELECT ParticipantID FROM participant WHERE participantPack = ". $fishypack['packID'].")";
$fish_query_result = query($fish_query);
$total = 0;
while($fish = mysqli_fetch_array($fish_query_result)){
$total = $total + $fish['fishLength'];
}
}
$total_query = "SELECT * FROM pack ORDER BY '$total' DESC";
$total_query_result = query($total_query);
$totalthing = mysqli_fetch_array($total_query_result);
?>
For the query I wrote regarding to my database, for the last 4 lines, the query isn't sorting pack by the order of $total descending. I think because the variable $total is including in the while loop and is not recognised outside the while loop, but I don't want my last 4 codes to be inside a loop. Is there a way to do use the variable?

Is that php, if yes then maybe you should try seeking an answer in a php forum.
Having said that, looking at the sql you are trying to order by $total which seems to be a number (correct me if I am wrong), which will sort based on ordinal position, so maybe you need to have a total column in pack and update it first with your $total variable.