从SQL输出的另一个数组中对信息数组进行排序


Sorting an array of information from another array output from SQL

我需要从一组数组数据中检索多个唯一值。目前提取如下:

while($row1 = mysql_fetch_array($result1))
    { 
     echo    "<tr>".
             "<td>".$row1[0]           . "</td>".
             "<td>".$row1[1]           . "</td>".
             "<td>".$row1[2]           . "</td>".
             "<td>".$row1[3]           . "</td>".        
             "<td>".$row1[4]           . "</td>".
             "<td>".$row1[5]           . "</td>".
             "<td>".$row1[6]           . "</td>".
             "<td>".$row1[7]           . "</td>".
//$row1[8] is the number of hours
             "<td>".$row1[8]           . "</td>".
//$row1[9] is the user
             "<td>".$row1[9]           . "</td>";
             }

如上所述,我需要累积每个用户的小时数。然而,我在排序数组时遇到了问题,因为用户值在数组中必须是唯一的,而数字必须保持堆叠。

现在真的很困惑。感谢您提供的任何帮助。

EDIT:$row1[8]是一个整数yes。

样本数据输出表(抱歉没有图像)如下:

------------------------------------------------------------------------------------------
Users |Telephone | Address | Postal Code | Hobbies | Interest| FB |Twitter | Insta | Hours
------------------------------------------------------------------------------------------
John  | 92238726 | SG      | 345322      | Running | Movies  |  1 |   0    | 0     |  5
Tom  | 922382134 | MY      | 345212      | Soccer | Movies  |  1 |   0    | 0      |  8
Jerry | 92238726 | SG      | 342122      | stamps | Nil  |  0 |   1    | 0      |   5
John  | 92238726 | SG      | 345322      | Running | Movies  |  1 |   0    | 0     |  12
Jerry | 92238726 | SG      | 342122      | stamps | Nil  |  0 |   1    | 0      |   2

根据上面用mysql_fetch_array提取的输出,我想将信息排序如下:

Users | Total Hours
John  |    17
Tom   |     8
Jerry |    7

SQL代码:

"select DISTINCT jl.refno, jl.logno, jl.attendee, jl.jobsummary, FROM_UNIXTIME(jl.date, '%d/%m/%y') AS 'Date-In', from_unixtime(jl.dateout + (15*3600), '%d/%m/%y') AS 'Date-Out', @timein := (left(jl.timein,2)*60+right(jl.timein,2)) AS 'Time-In', @timeout := (left(jl.timeout,2)*60+right(timeout,2)) AS 'Time-Out', @temp := ((dateout -date)* 24 * 60) + @timeout - @timein AS 'temp', us.username from joblog jl, projects proj, users us where jl.project ='{$value}' AND proj.id ='{$value}' AND jl.staff = us.id" 

由于您无论如何都在使用MySQL,我建议您使用SQL。它更干净。

将您的查询编辑为:

SELECT users, SUM(hours) as total FROM userTable GROUP BY users ORDER BY total DESC;

更新-PHP版本:

你可以在PHP中这样做。

$counters = array();
while ($row1 = mysql_fetch_array($result1)) {
    $counters[$row1[9]] += $rows1[8];
}
arsort($counters);
foreach ($counters as $name => $total) {
   // do your output here
}
    <?php
function totalHours($user){
$result=mysql_query("select sum(hours) as total  from table_name where users='".$user."'");
$row=mysql_fetch_array($result);
return $row['total'];
    }
$result=mysql_query("select * from table_name group by users");
echo "</table>";
echo "<tr>
<td>User</td>
<td>Total Hours</td>
</tr>";
while($row1 = mysql_fetch_array($result1))
    { 
 echo    "<tr>".
         "<td>".$row1[0]. "</td>".
        "<td>".totalHours($row1[0]). "</td></tr>";
         }
        echo "</table>";             
?>