我有一个PHP脚本,大部分运行正常。实际上,查询很棒,但此时我唯一的问题在于JSON部分。
在我下面的脚本的末尾,我有一个循环,将我的$data
语句视为一个数组并将其写入JSON文件,但由于某些原因,这对我来说现在是一个问题。
目前,内容数组和while循环只是从select语句中提取数据元素$d
,然后将数组写入json文件。但是,我现在有$stmt2
用另一个表中的ID更新我的表,因此它不包含在select语句中,我需要它在JSON文件中。如果我甚至可以使用另一个循环,我不知道我会怎么做。
我的另一个选择是让这个脚本运行所有查询,一旦完成,只需将表jfi.ambitionphone
转储到JSON文件,可能只将它限制为今天日期的记录。
另一个小问题是当前正在编写的JSON格式如下:
{
"0": {
"extension": "7200",
"Total_Talk_Time_seconds": "62",
"Total_Talk_Time_minutes": "1.03",
"Total_Outbound": "0",
"Total_Inbound": "0",
"Total_Missed": "1",
"Total_Calls": "1",
"Time_of_report": "2017-09-05 09:55:54",
"Date_of_report": "2017-09-05"
}, ......
当我需要这样的时候:
[
{
"ExtID" : 75,
"Extension" : 7218,
"Total_Talk_Time_seconds" : 1495,
"Total_Talk_Time_minutes" : 24.92,
"Total_Inbound" : 10,
"Total_Outbound" : 1,
"Missed_Calls" : 4,
"Total_Calls" : 11,
"Date_of_report" : "2017-08-15",
"Time_of_report" : "2017-08-15 16:13:30"
}, .......
所以,目前的主要问题是我需要我的JSON文件来包含已在下面的循环中编写的内容,但我现在需要添加(在'extension'之后)来自$stmt2
的
这是脚本本身:
<?php
$data = mysqli_query($conn, " SELECT c.extension
, sum(Duration) AS Total_Talk_Time_seconds
, round(sum(Duration) / 60,2) AS Total_Talk_Time_minutes
, sum(if(LEGTYPE1 = 1,1,0)) AS Total_Outbound
, sum(if(LEGTYPE1 = 2,1,0) and ANSWERED = 1) AS Total_Inbound
, sum(if(Answered = 1,0,1)) AS Total_Missed
, SUM(IF(LEGTYPE1 = 1, 1, 0)) + -- outbound
SUM(IF(LEGTYPE1 = 2, 1, 0) AND ANSWERED = 1) + -- inbound
SUM(IF(Answered = 1, 0, 1)) AS Total_Calls
, NOW() AS Time_of_report
, curdate() AS Date_of_report
FROM cdrdb.session a
INNER JOIN cdrdb.callsummary b
ON a.NOTABLECALLID = b.NOTABLECALLID
INNER join cdrdb.mxuser c
ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
WHERE b.ts >= curdate()
AND c.extension IN (7276,7314,7295,7306,7357,7200,7218,7247,7331,7255,7330,7000,7215,7240,7358,7312)
group by c.extension") or die(mysqli_error( $conn));
$stmt = mysqli_prepare($conn2, "Insert into jfi.ambitionphone(Extension, Total_Talk_Time_seconds, Total_Talk_Time_minutes, Total_Outbound, Total_Inbound,
Missed_Calls, Total_Calls, Date_of_report,Time_of_report )
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON duplicate key update
Total_Talk_Time_seconds = values(Total_Talk_Time_seconds),
Total_Talk_Time_minutes = values(Total_Talk_Time_minutes),
Total_Inbound = values(Total_Inbound),
Total_Outbound = values(Total_Outbound),
Missed_Calls = values(Missed_Calls),
Total_Calls = values(Total_Calls),
Time_of_report = NOW()
") or die(mysqli_error( $conn2));
$stmt2 = mysqli_prepare($conn2, "UPDATE jfi.ambitionphone a,
(SELECT * FROM jackson_id.users where id in
(48,75,76,77,81,82,83,85,86,87,99341,99344,99409,99422,99428)) j
SET a.ExtID = j.id where a.extension = j.extension") or die(mysqli_error($conn2));
echo print_r($stmt);
// Create the main array
$content = [];
while ($d = mysqli_fetch_array($data, MYSQLI_ASSOC)) {
mysqli_stmt_bind_param($stmt,"iidiiiiss", $d['extension'],
$d['Total_Talk_Time_seconds'], $d['Total_Talk_Time_minutes'],
$d['Total_Outbound'], $d['Total_Inbound'], $d['Total_Missed'], $d['Total_Calls'],
$d['Time_of_report'], $d['Date_of_report']) ;
mysqli_stmt_execute($stmt) or die(mysqli_error($conn2));
// Store the current row
$content[] = $d;
}
mysqli_stmt_execute($stmt2) or die(mysqli_error($conn2));
// Store it all
file_put_contents('ambitionLog.json', json_encode($content, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT));
?>