0

我正在尝试完成以下情况:

$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
";

我知道我无法通过 MySQL 语句中的函数完成排序,但我试图弄清楚如何获取所有返回的记录,然后从 php 函数结果中按从高到低的顺序对它们进行排序。任何想法将不胜感激;几个小时以来,我一直在努力解决这个问题...... :-(

        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;
    }
4

1 回答 1

0

你可能不得不做一些数组杂耍。首先将您的所有条目从 st_users 获取到第一个数组(mysql_query)然后您可以遍历该数组,并为每个条目执行 calculate_progress_percent() 并构建第二个数组,您可以在其中添加附加信息(“user_progress_percent” )。在此之后,您可以根据您的新信息(“user_progress_percent”)对新数组进行排序。

这里有一些快速而肮脏的代码建议——但是代码没有经过测试……当然……:)

第一的:

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

然后是这样的:

$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++;
}

然后应该可以对第二个数组进行排序,如下所述:

按值排序多维数组

…希望这在某些时候有所帮助…</p>

于 2013-01-18T00:32:27.457 回答