php - 我可以单独从服务器端 PHP 填充 "Google Charts"吗?

标签 php mysql codeigniter google-visualization

我正在使用 CodeIgniter 框架。在我的 View 中,我有下表,该表是通过 Controller 和模型从数据库中提取的数据自动填充的。我如何在 Google 图表中表示它们?我对 AJAX、JSON 等的了解较少,因此我现在无法进入 Google Chart API。是否有其他方法可以在 Google 图表或类似内容中表示它们?

<?php foreach ($query->result_array() as $row): { ?>
    <tr class="success">
        <td><?php echo $row['room_number'];?></td>
        <td><?php echo $row['start_date'];?></td>
        <td><?php echo $row['end_date'];?></td>
        <td><?php echo $row['pos_food_bev'];?></td>
        <td><?php echo $row['total'];?></td>
        <td><?php echo $row['payment_status'];?></td>
        <td><?php echo $row['guest_status'];?></td>
    </tr>    
<?php } ?>
<?php endforeach; ?>

最佳答案

如果您提供更多信息,那就太好了,这是我制作的条形图的一些代码,只是为了解释如何传递 php 数据以供 javascript 脚本使用

   <html>
  <head>



    <?php 
$hits = array(
    array("day"=>10,"count" =>53),
    array("day"=>11,"count" =>67),
    array("day"=>12,"count" =>85),
    array("day"=>13,"count" =>57),
    array("day"=>14,"count" =>65),
    array("day"=>15,"count" =>71),
    array("day"=>16,"count" =>85),
    array("day"=>17,"count" =>106),
    array("day"=>18,"count" =>55),
    array("day"=>19,"count" =>96),
    );
//this counter will be used later on the foreach
$counter = count($hits);?>
            <meta charset="utf-8">
            <script type="text/javascript" src="https://www.google.com/jsapi"></script>
            <script type="text/javascript">
              google.load("visualization", "1", {packages:["corechart"]});
              google.setOnLoadCallback(drawChart);
              function drawChart() {
                var data = google.visualization.arrayToDataTable([
                  ['Day',

 'Number of hits'],
              <?php foreach ($hits as $key =>$hit):?>

                <?php /*if the key is equal to the counter-1 it means we've reached
    the end of our array in that case the javascript array,
    won't have a comma at the end, or else it'll give a
    unexpected identifier error*/
                     if(($counter-1)==$key):?>
                  ['<?=$hit["day"]?>', <?=$hit["count"]?>]
                <?php else:?>
                  ['<?=$hit["day"]?>', <?=$hit["count"]?>],
                <?php endif;?>
                <?php endforeach;?>
            ]);

            var options = {
              title: 'Number of hits per day',
              hAxis: {title: 'Day', titleTextStyle: {color: 'blue'}}
            };

            var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
            chart.draw(data, options);
          }
        </script>
      </head>
      <body>
        <div id="chart_div" style="width: 900px; height: 500px;"></div>
      </body>
    </html>

关于php - 我可以单独从服务器端 PHP 填充 "Google Charts"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17627209/

相关文章:

javascript - iframe 中尾部外部文本文件

php - 如何在 Symfony 2 表单中验证日期时间格式 "yyyy-MM-ddTHH:mm:ss.sssZ"

PHP preg_replace 替换数字和特殊字符

php - stdClass 类的对象无法转换为字符串

mysql - 在 Codeigniter 中使用查询生成器 - 如何正确使用别名?

php - 使用codeigniter以单一形式将多个数据插入到mysql表中

php - 根据日期向表中插入行

javascript - moment.js "invalid date"计算时间差

php - 数据库中的 OpenID 重复结果

python:比较等效坐标