对于谷歌可视化,我手工编写了JSON(我知道凌乱)我没有想到'
NAME_TEAM
中最近出现问题的json_encode
作为我的第一个团队名称进入了撇号。我已阅读this post并尝试实施php new Date
。我对日期有疑问。 Fatal error: Class 'Date'
在旧的凌乱json格式中工作正常,但是当我尝试使用类似Json_encode版本的任何内容时,我收到错误foreach ($data as $row) {
$dateArray = explode('-', $row['THE_DATE']);
$year = $dateArray[0];
$month = $dateArray[1] - 1;
$day = $dateArray[2];
$dataArray[] = "[new Date ($year, $month, $day), {$row['ORANGE']}, {$row['GREEN']}, '{$row['NAME_TEAM']}']";
$itemOutput[] = array(
new Date($year, $month, $day),
$row['ORANGE_SCORE'],
$row['GREEN_SCORE'],
$row['NAME_TEAM'],
);
}
echo "data.addRows(" . json_encode($itemOutput) . ");" ;
。格式必须是可视化API的日期才能理解值。
function drawDashboard() {
//var data = new google.visualization.DataTable();
var data = new google.visualization.DataTable(<?php echo json_encode($itemOutput, JSON_NUMERIC_CHECK); ?>);
data.addColumn('date', 'Date');
data.addColumn('number', 'ORANGE_SCORE');
data.addColumn('number', 'GREEN_SCORE');
data.addColumn('string', 'NAME_TEAM');
// data.addRows(<?php echo '[' . implode(',', $dataArray) . ']'; ?>);
数据表创建
{"c":[
{"Date":"new Date(2013, 9, 19)"},
{"ORANGE_SCORE":14},
{"GREEN_SCORE":7},
{"NAME_TEAM":"Trigon"}]
},
ARRAY FORMAT
{{1}}
答案 0 :(得分:2)
如果是案例1或2,你想要什么?
结果是案例2。
<?
/***
CREATE TABLE `24992408` (
`THE_DATE` TIMESTAMP NOT NULL DEFAULT DATE ,
`ORANGE` VARCHAR( 50 ) NOT NULL ,
`ORANGE_SCORE` INT( 11 ) NOT NULL ,
`GREEN` VARCHAR( 50 ) NOT NULL ,
`GREEN_SCORE` INT( 11 ) NOT NULL ,
`NAME_TEAM` VARCHAR( 50 ) NOT NULL
) ENGINE = MYISAM ;
INSERT INTO `24992408` (
`THE_DATE` ,
`ORANGE` ,
`ORANGE_SCORE` ,
`GREEN` ,
`GREEN_SCORE` ,
`NAME_TEAM`
)
VALUES (
'2014-07-28' , '1', '1', '1', '2', 'NAME_TEAM'
);
***/
$host = "localhost";
$name = "user";
$db = "db";
$password ="password";
function confirm_query($watever){
global $connection;
if (!$watever) {
die("Database query failed! " . mysqli_error($connection));
}
}
function select_query(){
global $connection;
$query = "SELECT * FROM `24992408` ";
$admin_set = mysqli_query($connection, $query);
confirm_query($admin_set);
return $admin_set;
}
$connection = mysqli_connect($host, $name, $password, $db);
$row = mysqli_fetch_assoc($admin_set = select_query());
$dateArray = explode('-', $row['THE_DATE']);
$year = $dateArray[0]; //echo $year; //2014
$month = $dateArray[1] - 1; //echo $month; //6
$day = $dateArray[2]; //echo $day; //28
// IN PUT
//$date1 = Date('Ymd',strtotime(sprintf('%04d%02d%02d',$year, $month, $day) ) ); //case 1
$date1 = "new Date ($year, $month, $day)"; //case 2
$dataArray[] = "[$date1, {$row['ORANGE']}, {$row['GREEN']}, '{$row['NAME_TEAM']}']";
$itemOutput[] = array( $date1, $row['ORANGE_SCORE'], $row['GREEN_SCORE'], $row['NAME_TEAM'] );
echo "data.addRows(" . json_encode($dataArray) . ");" ;
// OUT PUT
//data.addRows(["[20140628, 1, 2, 'NAME_TEAM']"]); // case 1 output
//data.addRows(["[new Date (2014, 6, 28), 1, 2, 'NAME_TEAM']"]); // case 2 output
?>
答案 1 :(得分:2)
您不能在JSON中使用Date对象;传递日期的唯一方法是通过字符串,但DataTable.addRows
方法不会将字符串解析为Date对象,因此您必须转换为完整的JSON结构才能使其工作。这是一个例子:
$itemOutput = array(
'cols' => array(
array('type' => 'date', 'label' => 'Date'),
array('type' => 'number', 'label' => 'ORANGE_SCORE'),
array('type' => 'number', 'label' => 'GREEN_SCORE'),
array('type' => 'string', 'label' => 'NAME_TEAM')
),
'rows' => array()
);
foreach ($data as $row) {
$dateArray = explode('-', $row['THE_DATE']);
$year = $dateArray[0];
$month = $dateArray[1] - 1;
$day = $dateArray[2];
$itemOutput['rows'][] = array('c' => array(
array('v' => "Date($year, $month, $day)"),
array('v' => $row['ORANGE_SCORE']),
array('v' => $row['GREEN_SCORE']),
array('v' => $row['NAME_TEAM'])
));
}
然后在javascript:
var data = new google.visualization.DataTable(<?php echo json_encode($itemOutput, JSON_NUMERIC_CHECK); ?>);