我正在尝试以格式
从数据库中获取数据[' Wp-orders',11],
将在javascript中回显。我的代码在下面 -
<?php
$query = $this->db->query('SELECT *, count(DISTINCT order_id) as total
FROM wp_orders
WHERE order_status="processing"
GROUP BY webname
ORDER BY webname');
foreach ($query->result_array() as $row) {
$Cdata[] = "['".$row->webname."', ".$row->total."]";
}
?>
我希望在javascript Function {}
中回显 google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
//['Wp-orders', 11],
<?php echo $Cdata; ?>
]);
我不知道我在哪里做错了。
答案 0 :(得分:0)
您无法回显数组。也许你可以把它破坏成逗号分隔的字符串,如下所示:
<?php echo implode(',', $Cdata); ?>
修改
此外,您正在使用$query->result_array()
返回一个多维数组,但随后使用对象语法尝试访问这些字段。
将查询代码更改为使用$query->result()
代替:
foreach ($query->result() as $row) {
$Cdata[] = "['".$row->webname."', ".$row->total."]";
}
或者,您可以保留查询并使用数组访问语法:
foreach ($query->result_array() as $row) {
$Cdata[] = "['".$row['webname']."', ".$row['total']."]";
}
答案 1 :(得分:0)
<强> PHP:强>
$i = 0;
foreach ($query->result() as $row)
{
$Cdata[$i]['webname'] = $row->webname;
$Cdata[$i]['total'] = $row->total;
$i++;
}
JS:
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
<?php
$size = sizeof($Cdata);
for($i = 0; $i < $Cdata; $i++)
{
echo "['".$Cdata[$i]['webname']."', ".$Cdata[$i]['total']."]";
if($i != $size-1)
{
echo ",";
}
}
?>
]);
答案 2 :(得分:0)
如果您使用JS,则需要JSON
$a = array('sd', 'sds', 1);
echo json_encode($a); // ["sd","sds",1]
答案 3 :(得分:0)
试试这个
foreach ($query->result_array() as $row) {
if($row->webname && $row->total){
$Cdata[] = array($row->webname=>$row->total);
}
}
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
<?php
echo json_encode($Cdata);
?>
]);