MySQL用PHP排序

I'm trying to accomplish the following situation:

$mysql_query = "
  SELECT * 
  FROM st_users 
  WHERE 
      `user_comp_supervisor_id` = '$team_supervisor' AND 
      `user_exempt_from_goals` = '0' 
  ORDER BY 'calculate_progress_percent()' ASC
";

I know that I can't accomplish ordering by a function in a MySQL statement, but I'm trying to figure out how to take all the returned records, and then order them in order of highest to lowest from a php function result. Any ideas would be greatly appreciated; I've been trying to wrap my head around this for a few hours now... :-(

        function diy_calc_progress_percent($user_id,$period_id,$period_week_number)
    {
        $this->user_id = $user_id;
        $this->period_id = $period_id;
        $this->period_week_number = $period_week_number;

        if ($this->period_week_number == 1)
        {
            $this->week_id = mysql_result( mysql_query(" SELECT `period_week_one` FROM `st_comp_periods` WHERE `period_id` = '$this->period_id' "),0 );
        }
        else if ($this->period_week_number == 2)
        {
            $this->week_id = mysql_result( mysql_query(" SELECT `period_week_two` FROM `st_comp_periods` WHERE `period_id` = '$this->period_id' "),0 );
        } 
        else
        {
            echo "Week number not valid.";
            exit();
        }

        $this->week_start_date = mysql_result( mysql_query(" SELECT `week_start_date` FROM `st_comp_weeks` WHERE `week_id` = '$this->week_id' "),0 );

        $this->week_end_date = mysql_result( mysql_query(" SELECT `week_end_date` FROM `st_comp_weeks` WHERE `week_id` = '$this->week_id' "),0 );

        $this->user_department = $this->user_info($this->user_id,"user_comp_department_id");

        $this->user_week_diy_goal = mysql_result( mysql_query(" SELECT `goal_diy_department` FROM `st_comp_department_goals` WHERE `goal_department_id` = '$this->user_department' AND `goal_week_id` = '$this->week_id' "),0 );

        $this->calc_totals_result = mysql_query("SELECT SUM(record_total_diy_revenue) AS user_week_total FROM `st_entered_records` WHERE `record_user_id` = '$this->user_id' AND `record_date` BETWEEN '$this->week_start_date' AND '$this->week_end_date'");
        $this->calc_totals_row = mysql_fetch_assoc($this->calc_totals_result);
        $this->user_week_total = $this->calc_totals_row['user_week_total']; 
        $this->user_week_one_percent = ($this->user_week_total / $this->user_week_diy_goal) * 100;
        $this->user_week_one_percent = number_format( (float)$this->user_week_one_percent, 2, '.', '' );

        return $this->user_week_one_percent;
    }

You probably will have to do some array juggling. First get all your entries FROM st_users into a first array (mysql_query) Then you could run through that array, and for each entry you do the calculate_progress_percent() and build up a second array in which you could add the additional info ("user_progress_percent"). After this you can sort the new array ba your new info ("user_progress_percent").

And here is some quick and dirty code-suggestions – code is however not tested… of course…:)

First:

$mysql_query = "SELECT * FROM st_users 
WHERE `user_comp_supervisor_id`='$team_supervisor' AND 
      `user_exempt_from_goals` = '0'";

Then something like this:

$i = 0;
while($tmp = mysql_fetch_array($mysql_query)) {
    $my_second_array[$i]['user_id'] = $tmp['user_id'];
    $user_id = $my_second_array[$i]['user_id'];
    diy_calc_progress_percent($user_id,$period_id,$period_week_number);
    $my_second_array[$i]['user_result'] = $diy_calc_progress_percent_result;
    $i++;
}

And then sorting that second array should be possible as described here:

Sort Multi-dimensional Array by Value

…hope this helps at some point…