Firstly, I notice that you used this branching logic twice:
if ($scorepf>=42) {$pointspf=1;}
elseif ($scorepf<=41 AND $scorepf>=29) {$pointspf=2;}
elseif ($scorepf<=28 AND $scorepf>=22) {$pointspf=3;}
elseif ($scorepf<=21 AND $scorepf>=15) {$pointspf=4;}
elseif ($scorepf<=14 AND $scorepf>=11) {$pointspf=5;}
elseif ($scorepf<=10 AND $scorepf>=8) {$pointspf=6;}
elseif ($scorepf<=7 AND $scorepf>=4) {$pointspf=7;}
elseif ($scorepf<=3 AND $scorepf>=2) {$pointspf=8;}
elseif ($scorepf>=1) {$pointspf=9;}
else {$pointspf=10;}
For one thing, you need not specify both ends of the range. In fact, you could reduce the logic to use an array, and write a function for it:
function computePoints($score)
{
static $score_mins = array(42, 29, 22, 15, 11, 8, 4, 2, 1);
for ($i = 0; $i < count($score_mins); ++$i)
{
if ($score >= $score_mins[$i])
{
return $i + 1;
}
}
return 10; // The default case.
}
$score_mins is declared static since we do not want to build it again on subsequent calls. Now, calculating the points and total points is easy:
$points = array();
while ($row = mysql_fetch_assoc($result))
{
$pointsoc = ($row['outcome'] == $row['resoutcome']) ? 5 : 0;
$pointspf = computePoints($row['pfscore']);
$pointspa = computePoints($row['pascore']);
$totalpoints = $pointsoc + $pointspf + $pointspa;
$points[] = array('soc' => $pointsoc,
'spf' => $pointspf,
'spa' => $pointspa,
'total' => $totalpoints,
'userid' => $row['userid'],
'username' => $row['username']);
}
// Sort by total points.
usort($points, create_function('$a,$b', 'return $a["total"]-$b["total"];'));
// Print out sorted list.
for ($i = 0; $i < count($points); ++$i)
{
// Print $points[$i]['soc'] etc.
}
Since you want to sort by total points, I used [man]usort/man with [man]create_function/man so that the comparison is done with the total points.