我正在尝试修改php脚本,以便在每15分钟运行一次时写入JSON文件,然后覆盖更新的结果。我的脚本是在数据库中执行此操作(在复制时插入和更新),但我的JSON文件代码运行不正常。
当我从工作台手动导出到JSON时,我得到了这个(这就是我想要的):
[
{
"ID" : 1,
"Extension" : 7218,
"ExtID" : 35302,
"Total_Talk_Time_seconds" : 11,
"Total_Talk_Time_minutes" : 0.18,
"Total_Inbound" : 0,
"Total_Outbound" : 1,
"Missed_Calls" : 0,
"Total_Calls" : null,
"Date_of_report" : "2017-08-15",
"Time_of_report" : "2017-08-15 08:58:31"
},
{
"ID" : 2,
"Extension" : 7306,
"ExtID" : 35370,
"Total_Talk_Time_seconds" : 762,
"Total_Talk_Time_minutes" : 12.7,
"Total_Inbound" : 4,
"Total_Outbound" : 0,
"Missed_Calls" : 3,
"Total_Calls" : null,
"Date_of_report" : "2017-08-15",
"Time_of_report" : "2017-08-15 08:58:31"
},
{
"ID" : 3,
"Extension" : 7358,
"ExtID" : 35278,
"Total_Talk_Time_seconds" : 41,
"Total_Talk_Time_minutes" : 0.68,
"Total_Inbound" : 0,
"Total_Outbound" : 0,
"Missed_Calls" : 0,
"Total_Calls" : null,
"Date_of_report" : "2017-08-15",
"Time_of_report" : "2017-08-15 08:58:31"
}
]
但是当我在下面运行我的实际PHP脚本时,我得到了这个:
{"extension":"7358","RESPONSIBLEUSEREXTENSIONID":"35278","Total_Talk_Time_seconds":"41","Total_Talk_Time_minutes":"0.68","Total_Outbound":"0","Total_Inbound":"0","Total_Missed":"0","Total_Calls":"0","time":"2017-08-15 10:05:07","date":"2017-08-15"}
所以,它最终会写出结果,但它只是6条记录中的一条,而且它是表中的第三条记录,所以它对我来说没有意义,为什么它会将第三条记录拉成一条记录。它也没有通过为每个值赋予一个新行来格式化为JSON,但JSON对我来说非常陌生。
PHP脚本运行没有错误并正确插入我的数据库,我只需要让这个脚本将结果写入JSON文件并在每15分钟运行时覆盖一次。
$data = mysqli_query($conn, " SELECT c.extension
,RESPONSIBLEUSEREXTENSIONID
, 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)) AS Total_Inbound
, sum(if(Answered = 1,0,1)) AS Total_Missed
, count(DISTINCT b.NOTABLECALLID) AS total_calls
, NOW() AS time
, curdate() AS date
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 (7295,7306,7218,7247,7330,7000,7358)
group by c.extension");
$stmt = mysqli_prepare($conn2, "Insert into jfi.ambitionphone(Extension, ExtID, Total_Talk_Time_seconds,
Total_Talk_Time_minutes,Total_Outbound, Total_Inbound,
Missed_Calls, Total_Calls, Time_of_report,Date_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_Outbound = values(Total_Outbound),
Total_Inbound = values(Total_Inbound),
Missed_calls = values(Missed_Calls),
Total_Calls = values(Total_Calls),
Time_of_report = values(Time_of_report),
Date_of_report = values(Date_of_report)") or die(mysqli_error($conn2));
foreach ($data as $d) {
mysqli_stmt_bind_param($stmt,"iiidiiiiss", $d['extension'], $d['RESPONSIBLEUSEREXTENSIONID'],
$d['Total_Talk_Time_seconds'], $d['Total_Talk_Time_minutes'],
$d['Total_Outbound'], $d['Total_Inbound'], $d['Total_Missed'], $d['Total_Calls'],
$d['time'], $d['date']) or die(mysqli_error($conn2));
mysqli_stmt_execute($stmt) or die(mysqli_error($conn2));
$ambitionJSONdata = json_encode($d);
file_put_contents('ambitionLog.json', $ambitionJSONdata);
}
我可以在这里做些什么来获取输出,就像我进行手动导出一样?
答案 0 :(得分:1)
您目前正在每次迭代时覆盖该文件。创建一个主数组来存储每个迭代:
// Create the main array
$content = [];
while ($d = mysqli_fetch_array($data, MYSQLI_ASSOC)) {
mysqli_stmt_bind_param($stmt,"iiidiiiiss", $d['extension'], $d['RESPONSIBLEUSEREXTENSIONID'],
$d['Total_Talk_Time_seconds'], $d['Total_Talk_Time_minutes'],
$d['Total_Outbound'], $d['Total_Inbound'], $d['Total_Missed'], $d['Total_Calls'],
$d['time'], $d['date']) or die(mysqli_error($conn2));
mysqli_stmt_execute($stmt) or die(mysqli_error($conn2));
// Store the current row in the main array
$content[] = $d;
}
// Store the main array that contains all the rows
file_put_contents('ambitionLog.json', json_encode($content));
答案 1 :(得分:-1)
只需在您的输出区域使用此行 echo json_encode $ your_final_array;