我尝试创建一个基于列名对项目进行分组的数据表。为了正确填充表格,我需要使用父子关系格式化JSON,但遇到了麻烦。我将通过PHP从MSSQL中获取数据。
$ xmodmap -e "remove Control = Control_L"
我需要像这样格式化的JSON,父对象位于"数据"和孩子们在孩子们的照顾下#34;我很难搞清楚我是否需要进行两个单独的查询,一个用于父级的汇总数据,另一个用于子行。任何帮助都会被提及。
编辑以添加PHP :
+---------------+-------+--------------+------------+------------+--------+
| ACTIVITY_NAME | GROUP | START_DATE | END_DATE | COMPLETED | TOTAL |
+---------------+-------+--------------+------------+------------+--------+
| Test | 1 | 04/30/2015 | 05/01/2015| 10 | 15 |
| Test | 2 | 04/30/2015 | 05/01/2015| 20 | 25 |
| Test2 | 1 | 05/2/2015 | 05/03/2015| 30 | 35 |
| Test2 | 2 | 05/2/2015 | 05/03/2015| 40 | 45 |
| Test2 | 3 | 05/2/2015 | 05/03/2015| 50 | 55 |
+---------------+-------+--------------+------------+------------+--------+
我需要这种格式: JSON blob:https://jsonblob.com/554b958be4b05c281ae9707e
<?php
include("connect.php");
if( $conn === false ) {
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. */
$sql = "<QUERY>";
$stmt = sqlsrv_query( $conn, $sql);
do {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$json[] = $row;
}
} while ( sqlsrv_next_result($stmt) );
foreach ($json as $result) {
$data[$result['ACTIVITY_NAME']]['children'] = $result;
}
echo json_encode($data);
?>
答案 0 :(得分:1)
我建议您使用两个查询,然后将数据放入与您的格式匹配的数组中,并将其json_encode以生成所需的结果。
您可能需要查看SQL Server recursive query
答案 1 :(得分:1)
您似乎正在通过ACTIVITY_NAME进行分组,如果是这种情况并且您不想进行其他查询,则可以先创建一个关联数组:
foreach ($results as $result) {
$data[$result['ACTIVITY_NAME']]['children'] = $result;
}
然后你可以使用该数组迭代子数来计算数据,如MAINTENANCE_GROUP,COMPLETED和TOTAL;
完成所有这些后,您可以使用array_values来获取非关联数组。
答案 2 :(得分:0)
以下是工作脚本的样子:
<?php
include("connect.php");
if( $conn === false ) {
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. */
$sql = "<query> ";
$stmt = sqlsrv_query($conn, $sql);
// This is where the data will be organized.
// It's better to always initialize the array variables before putting data in them
$data = array();
// Get the rows one by one
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
// Extract the activity name; we want to group the rows by it
$name = $row['ACTIVITY_NAME'];
$group = '';
$sdate = '';
$edate = '';
$completed = '';
$total = '';
$perc = '';
// Check if this activity was encountered before
if (! isset($data[$name])) {
// No, this is the first time; we will make room for it, first
$data[$name] = array(
// Remember the name
'ACTIVITY_NAME' => $name,
'MAINTENANCE_GROUP' => $group,
'START_DATE' => $sdate,
'END_DATE' => $edate,
'COMPLETED' => $completed,
'TOTAL_CLUSTERS' => $total,
'COMPLETE_PERC' => $perc,
// No children yet
'children' => array(),
);
}
// Put the row into the list of children for this activity
$data[$name]['children'][] = $row;
}
// Here, the entries in $data are indexed by the values they also have in 'ACTIVITY_NAME'
// If you want them numerically indexed, all you have to do is:
$data = array_values($data);
echo json_encode(array('data' => $data));
//echo json_encode($data);
?>