来自数组

时间:2017-10-09 23:11:54

标签: php mysql arrays

我有一个像这样的数据库:

id | start               | stop                | reason
1  | 2017-10-09 08:00:00 | 2017-10-09 08:15:00 | fault 1
2  | 2017-10-09 08:15:00 | 2017-10-09 08:30:00 | fault 2
3  | 2017-10-09 08:30:00 | 2017-10-09 09:00:00 | fault 3
4  | 2017-10-09 10:00:00 | 2017-10-09 11:15:00 | fault 1
5  | 2017-10-09 14:00:00 | 2017-10-09 15:00:00 | fault 2

我想使用flot图表制作这些数据的饼图,但我需要先对数据进行分组,然后只显示总持续时间百分比,从而产生如下数组:

0   
label:  "Fault 1"
data:   46.15385
1   
label:  "Fault 2"
data:   38.46154
2   
label:  "Fault 3"
data:   15.38462

我无法对它们进行分组,我认为我可以使用in_array但是我遇到了麻烦,到目前为止这是我的代码: -

header('Content-Type: application/json');
$sql = "SELECT * FROM " . STATE_TABLE . " INNER JOIN " . DOWNTIME_TABLE . 
    " ON " . STATE_TABLE . ".reason = " . DOWNTIME_TABLE . ".faultId
    WHERE ('$shiftEnd' > start AND stop > '$shiftStart')
    OR ('$shiftEnd' > start AND stop is null) ORDER BY start DESC";
if ( !($result = $db->sql_query($sql)) )
{
    die('Could not query STATUS_TABLE database');
}
$totalDowntime = 0;
while( $row = $db->sql_fetchrow($result) ) {
    $start = $row['start'];
    $stop = $row['stop'] == null ? $currentTime : $row['stop'];
    $faultStart = $shiftStart > $start ? $shiftStart : $start;
    $faultStop = $stop > $shiftEnd ? $shiftEnd : $stop;

    $duration = get_time_difference($faultStart, $faultStop, 'seconds');

    $totalDowntime += $duration;
    $faults[] = $row;
}
$db->sql_freeresult($result);

$output = array();
$i = 0;
foreach($faults as $fault) {

    $start = $fault['start'];
    $stop = $fault['stop'] == null ? $currentTime : $fault['stop'];
    $description = $fault['description'];   
    $faultStart = $shiftStart > $start ? $shiftStart : $start;
    $faultStop = $stop > $shiftEnd ? $shiftEnd : $stop;

    $duration = get_time_difference($faultStart, $faultStop, 'seconds');

    $percent = ($duration / $totalDowntime) * 100;

    if (in_array($description, $output)) {
        $output[]['data'] += $percent;
    } else {
        $output[$i]['label'] = $description;
        $output[$i]['data'] = $percent;
    }
    $i++;
}

die(json_encode($output));

1 个答案:

答案 0 :(得分:0)

您是否无法使用SQL查询对数据进行分组,以便您可以直接从MySQL进行绘图?

SQL小提琴:http://sqlfiddle.com/#!9/152bcc/4/0

对于这样的表:

create table faults (id int not null, start timestamp not null, stop timestamp, reason text not null);
insert into faults (id, start, stop, reason) values 
(1, '2017-10-09 08:00:00', '2017-10-09 08:15:00', 'fault 1'),
(2, '2017-10-09 08:15:00', '2017-10-09 08:30:00', 'fault 2'),
(3, '2017-10-09 08:30:00', '2017-10-09 09:00:00', 'fault 3'),
(4, '2017-10-09 10:00:00', '2017-10-09 11:15:00', 'fault 1'),
(5, '2017-10-09 14:00:00', '2017-10-09 15:00:00', 'fault 2'),
(6, '2017-10-09 14:00:00',  NULL                , 'fault 2');

您可以通过"原因"使用TIMEDIFF()功能和分组。柱。 COALESCE()函数返回列表中的第一个NOT NULL值,因此当stop为空时,它可用于替换当前时间戳:

select 
  reason as label,
  SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(COALESCE(stop, CURRENT_TIMESTAMP), start)))) as data,
  SUM(TIME_TO_SEC(TIMEDIFF(COALESCE(stop, CURRENT_TIMESTAMP), start))) as data_seconds
from faults
group by reason 
order by reason;

|   label |     data | data_seconds |
|---------|----------|--------------|
| fault 1 | 01:30:00 |         5400 |
| fault 2 | 13:23:02 |        48182 |
| fault 3 | 00:30:00 |         1800 |