SQL 数据 - 以特定格式编码 json
SQL data - encode json in particular format
我有 php 代码与 sql 一起使用,以根据 EA 编号和 ID 编号从数据库中获取结果以绘制图表。
<?php
$sql=
("SELECT D as y,Total as a,PoweredOn as b
FROM MasterUsageTable
WHERE ID IN
(SELECT ID FROM MasterUsageTable
WHERE EA_Number = '$valueEA' AND D BETWEEN #$valueSDate# AND #$valueEDate#)");
$rs = odbc_exec($con, $sqlVMs);
if (!$rs) {
exit("Enter EA!");
}
$data = array();
while ($row = odbc_fetch_array($rs)){
$data[] = $row;
}
echo json_encode($data);
?>
目前我在 json 编码后得到结果的方式是这样的:
[{"y":"2015-03-26","a":"16","b":"14"},{"y":"2015-03-26
","a":"59","b":"12"},{"y":"2015-03-26
","a":"21","b":"5"},{"y":"2015-03-26
","a":"35","b":"12"},{"y":"2015-03-26 ","a":"17","b":"2"}]
我希望有一些方法可以像这样将它们分开:
[{"y":"2015-03-26","a":"16","b":"14"},{"y":"2015-03-26
","a":"59","b":"12"},{"y":"2015-03-26
","a":"21","b":"5"}],[{"y":"2015-03-26
","a":"35","b":"12"},{"y":"2015-03-26 ","a":"17","b":"2"}]
因此它是基于其 ID 的多个数据数组。希望有道理,谢谢
一种方法是使用按 ID 的外循环和内循环来检索每个 ID 值的值,如下所示:
<?php
// test data
$valueSDate = "2015-03-26";
$valueEDate = "2015-03-26";
$valueEA = "EA1";
$allData = array();
$con = odbc_connect(
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" .
"Dbq=C:\Users\Public\Database1.accdb;",
"Admin", "");
$rs1 = odbc_exec($con,
"SELECT DISTINCT ID " .
"FROM MasterUsageTable " .
"WHERE EA_Number = '$valueEA' " .
"AND D BETWEEN #$valueSDate# AND #$valueEDate#");
while ($row1 = odbc_fetch_array($rs1)) {
$id = $row1["ID"];
$rs2 = odbc_exec($con,
"SELECT D as y,Total as a,PoweredOn as b " .
"FROM MasterUsageTable " .
"WHERE ID = $id AND EA_Number = '$valueEA' " .
"AND D BETWEEN #$valueSDate# AND #$valueEDate#");
$idData = array();
while ($row2 = odbc_fetch_array($rs2)) {
$idData[] = $row2;
}
$allData[] = $idData;
}
echo json_encode($allData);
我有 php 代码与 sql 一起使用,以根据 EA 编号和 ID 编号从数据库中获取结果以绘制图表。
<?php
$sql=
("SELECT D as y,Total as a,PoweredOn as b
FROM MasterUsageTable
WHERE ID IN
(SELECT ID FROM MasterUsageTable
WHERE EA_Number = '$valueEA' AND D BETWEEN #$valueSDate# AND #$valueEDate#)");
$rs = odbc_exec($con, $sqlVMs);
if (!$rs) {
exit("Enter EA!");
}
$data = array();
while ($row = odbc_fetch_array($rs)){
$data[] = $row;
}
echo json_encode($data);
?>
目前我在 json 编码后得到结果的方式是这样的:
[{"y":"2015-03-26","a":"16","b":"14"},{"y":"2015-03-26 ","a":"59","b":"12"},{"y":"2015-03-26 ","a":"21","b":"5"},{"y":"2015-03-26 ","a":"35","b":"12"},{"y":"2015-03-26 ","a":"17","b":"2"}]
我希望有一些方法可以像这样将它们分开:
[{"y":"2015-03-26","a":"16","b":"14"},{"y":"2015-03-26 ","a":"59","b":"12"},{"y":"2015-03-26 ","a":"21","b":"5"}],[{"y":"2015-03-26 ","a":"35","b":"12"},{"y":"2015-03-26 ","a":"17","b":"2"}]
因此它是基于其 ID 的多个数据数组。希望有道理,谢谢
一种方法是使用按 ID 的外循环和内循环来检索每个 ID 值的值,如下所示:
<?php
// test data
$valueSDate = "2015-03-26";
$valueEDate = "2015-03-26";
$valueEA = "EA1";
$allData = array();
$con = odbc_connect(
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" .
"Dbq=C:\Users\Public\Database1.accdb;",
"Admin", "");
$rs1 = odbc_exec($con,
"SELECT DISTINCT ID " .
"FROM MasterUsageTable " .
"WHERE EA_Number = '$valueEA' " .
"AND D BETWEEN #$valueSDate# AND #$valueEDate#");
while ($row1 = odbc_fetch_array($rs1)) {
$id = $row1["ID"];
$rs2 = odbc_exec($con,
"SELECT D as y,Total as a,PoweredOn as b " .
"FROM MasterUsageTable " .
"WHERE ID = $id AND EA_Number = '$valueEA' " .
"AND D BETWEEN #$valueSDate# AND #$valueEDate#");
$idData = array();
while ($row2 = odbc_fetch_array($rs2)) {
$idData[] = $row2;
}
$allData[] = $idData;
}
echo json_encode($allData);