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

时间:2014-04-23 14:06:20

标签: php

我需要从数组数据集中检索多个唯一值。目前它们的摘录如下:

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

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

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

编辑:$ row1 [8]是一个整数是。

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

------------------------------------------------------------------------------------------
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 CODE:

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

2 个答案:

答案 0 :(得分:0)

由于您仍在使用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
}

答案 1 :(得分:0)

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