多个记录到Google图表 [英] Multiple records to Google Charts

查看:91
本文介绍了多个记录到Google图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Google图表的帮助下通过SQL查询创建时间轴.我已使用以下代码创建了编码为json的数组.问题是,$ table仅包含此代码末尾的最后一条记录,但是我有1条以上的记录.

I would like to make a timeline from a SQL query with the help of Google Charts. I have used the following code to create the array, that encode to json. The problem is, that $table contains only the last record on the end of this code, but I have more than 1 record.

$rows = array();
$table = array();

$table['cols'] = array( 
  array('id' => '', 'label' => 'Name', 'pattern' => '', 'type' => 'string'), 
  array('id' => '', 'label' => 'Start', 'pattern' => '', 'type' => 'date'), 
  array('id' => '', 'label' => 'End', 'pattern' => '', 'type' => 'date') ); 
while($r = mysql_fetch_assoc($results)) { 
  $name=$r['Name']; $date=$r['Date'];
  $start=$r['start']; 
  $end=$r['end']; 
  $year=date("Y", strtotime($date)); 
  $month=date("m", strtotime($date)); 
  $day=date("d", strtotime($date));
  $start_h=date("H", strtotime($start));
  $start_min=date("i", strtotime($start));
  $start_sec=date("s", strtotime($start)); 
  $end_h=date("H", strtotime($end));
  $end_min=date("i", strtotime($end)); 
  $end_sec=date("s", strtotime($end));
  $start_merged = "Date(".$year.",".$month.",".$day.",".$start_h.",".$start_min.",".$start_sec.")";
  $end_merged = "Date(".$year.",".$month.",".$day.",".$end_h.",".$end_min.",".$end_sec.")";
  $rows = array(); 
  $temp = array(); 
  $temp[] = array('v' => (string) $name); 
  $temp[] = array('v' => (string) $start_merged); 
  $temp[] = array('v' => (string) $end_merged);
  $rows[] = array('c' => $temp);
}
$table['rows'] = $rows;

推荐答案

在循环中间清空要在其中收集结果的数组:

You empty the array in which you collect the results in the middle of the loop:

$rows = array();

将其移到外面.

免责声明:我没有检查其余的代码.

Disclaimer: I haven't checked the rest of the code.

注意:明智地使用var_dumpprint_r可以快速向您显示问题出在哪里.

Note: judicious use of var_dump or print_r can quickly show you where the problem occours.

这篇关于多个记录到Google图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆