1
내 로컬 디렉토리의 .json 파일에 db 쿼리 (SQL 서버)를 출력하는 코드가 있습니다.JSON 데이터가있는 모리스 차트
$connectionInfo = array("");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if($conn)
{
}else
{ echo "Connection could not be established.\n";
die(print_r(sqlsrv_errors(), true));
}
$sql="";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if($query === false) {
echo "Error in executing query.</br>";
die(print_r(sqlsrv_errors(), true));
}
$json = array();
do {
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$json[] = $row;
}
} while (sqlsrv_next_result($query));
file_put_contents('data.json', json_encode($json));
이 부분은 정상적으로 작동하며이 형식의 json 데이터를 출력합니다. 다른 페이지
[{"month":1,"a":43,"b":10},{"month":2,"a":43,"b":87},{"month":3,"a":52,"b":11},{"month":4,"a":66,"b":25},{"month":5,"a":30,"b":11},{"month":6,"a":29,"b":10},{"month":7,"a":32,"b":41},{"month":8,"a":27,"b":36},{"month":9,"a":36,"b":32},{"month":10,"a":57,"b":57},{"month":11,"a":32,"b":20},{"month":12,"a":53,"b":65}]
은이 같은 상기 JSON 데이터와이 모리스 영역 차트 초기화있어 :
먼저 나 배열을 통해 JSON 파일 루프를 판독하고있다. (document.url)를 사용하여 동일한 페이지에 Ajax를 사용 모리스 차트 초기화
$final = json_decode(file_get_contents('data.json'));
$new_final = array();
foreach($final as $value) {
foreach($value as $sub_value) {
$new_final[] = $sub_value;
}
}
echo json_encode($new_final);
exit;
}
둘째 임 :
$(document).ready(function(){
$.ajax({
url: document.URL,
dataType: 'JSON',
type: 'POST',
data: {get_values: true},
success: function(response) {
Morris.Area({
element: 'area-chart',
data: response,
xkey: 'month',
ykeys: ['a','b'],
labels: ['Month', 'Total']
});
}
});
});
IT는 차트를 출력하지있다. 나는 이런 식으로, 당신은 .JSON 파일을 조작하고 추가 '[]'이 제대로 작동하고 출력됩니다 추가 할 때 예상처럼 것을 발견 않았다
[[{"month":1,"a":43,"b":10},{"month":2,"a":43,"b":87},{"month":3,"a":52,"b":11},{"month":4,"a":66,"b":25},{"month":5,"a":30,"b":11},{"month":6,"a":29,"b":10},{"month":7,"a":32,"b":41},{"month":8,"a":27,"b":36},{"month":9,"a":36,"b":32},{"month":10,"a":57,"b":57},{"month":11,"a":32,"b":20},{"month":12,"a":53,"b":65}]]
누군가 내가 여기에 감독하고있는 무슨 말해 수 ? 두 번째 배열이 필요한 것 같지만 잘 모르겠습니다 .. 미리 감사드립니다.