同事们的程序员,晚安。我很难用PHP,MySQL和Highcharts库整理每周记录图表。
我有连接和断开连接的记录,其中以 2017-03-30 16:43:04 的格式保存,但在图表的行中我需要显示此信息为期7天。
例如今天一周的开头:横坐标轴将包含7个值 03 / 04,04 / 04,04 / 04,06 / 04,07 / 04,08 / 04和09/04 并且纵坐标的轴将具有表示一定量连接或断开的整数值。
我不知道如何在后端生成这些数据,尤其是SQL查询。
表格列:
id | username | event | date
id 是事件 ID,用户名是用户名,事件可以采用连接或断开连接值,日期是 2017-03-30 16:43:04 格式的日期。
图表示例:
我做的丑陋的方式:
获取6天前+今天的DD / MM格式:
$date['timestamp'] = array(
'0' => $this->db->select("DATE_ADD(CURDATE(), INTERVAL - 6 - WEEKDAY(CURDATE()) DAY) AS dateX")->get()->row()->dateX,
'1' => $this->db->select("DATE_ADD(CURDATE(), INTERVAL - 5 - WEEKDAY(CURDATE()) DAY) AS dateX")->get()->row()->dateX,
'2' => $this->db->select("DATE_ADD(CURDATE(), INTERVAL - 4 - WEEKDAY(CURDATE()) DAY) AS dateX")->get()->row()->dateX,
'3' => $this->db->select("DATE_ADD(CURDATE(), INTERVAL - 3 - WEEKDAY(CURDATE()) DAY) AS dateX")->get()->row()->dateX,
'4' => $this->db->select("DATE_ADD(CURDATE(), INTERVAL - 2 - WEEKDAY(CURDATE()) DAY) AS dateX")->get()->row()->dateX,
'5' => $this->db->select("DATE_ADD(CURDATE(), INTERVAL - 1 - WEEKDAY(CURDATE()) DAY) AS dateX")->get()->row()->dateX,
'6' => $this->db->select("DATE_ADD(CURDATE(), INTERVAL + 0 - WEEKDAY(CURDATE()) DAY) AS dateX")->get()->row()->dateX
);
根据上述日期获取连接和断开连接:
$date['series'] = array(
'con' => array(
'0' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'conexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 6 DAY')->get()->row()->dateX,
'1' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'conexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 5 DAY')->get()->row()->dateX,
'2' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'conexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 4 DAY')->get()->row()->dateX,
'3' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'conexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 3 DAY')->get()->row()->dateX,
'4' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'conexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 2 DAY')->get()->row()->dateX,
'5' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'conexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 1 DAY')->get()->row()->dateX,
'6' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'conexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 0 DAY')->get()->row()->dateX
),
'dis' => array(
'0' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'desconexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 6 DAY')->get()->row()->dateX,
'1' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'desconexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 5 DAY')->get()->row()->dateX,
'2' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'desconexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 4 DAY')->get()->row()->dateX,
'3' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'desconexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 3 DAY')->get()->row()->dateX,
'4' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'desconexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 2 DAY')->get()->row()->dateX,
'5' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'desconexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 1 DAY')->get()->row()->dateX,
'6' => $this->db->select('COUNT(id_ConnectionLog) AS dateX')->from('connection_log')->where('cl_Event', 'desconexão')->where('cl_Client', $client_name)->where('date(cl_Date) = CURDATE() - INTERVAL 0 DAY')->get()->row()->dateX
)
);