0

我有点卡住 Javascript 来阅读我的 PHP 代码

到目前为止,我需要用脚本顶部的 PHP 变量替换 google 图表的 javascript 中的数字。

任何帮助都会很棒

<?php

$jan = 'SQL Code for Jan';
$feb = 'SQL Code for Feb';
$mar = 'SQL Code for Mar';
$apr = 'SQL Code for Apr';
$may = 'SQL Code for May';
$jun = 'SQL Code for Jun';
$jul = 'SQL Code for Jul';
$aug = 'SQL Code for Aug';
$sept = 'SQL Code for Sept';
$oct = 'SQL Code for Oct';
$nov = 'SQL Code for Nov';
$dec = 'SQL Code for Dec';

?>

<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([
      ['Month', '# of Logins'],
      ['Jan',  999; ],
      ['Feb',  1170],
      ['Mar',  660],
  ['Apr',  660],
  ['May',  660],
  ['Jun',  660],
  ['Jul',  660],
  ['Aug',  660],
  ['Sept',  660],
  ['Oct',  660],
  ['Nov',  660],
      ['Dec',  1030]
    ]);

    var options = {
      title: 'Number of Logins'
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>

<div id="chart_div" style="width: 900px; height: 500px;"></div>
4

1 回答 1

0

如果我正确阅读了您的请求,您需要将660in['Apr', 660],替换为$apr. 我知道的最简单的方法是让您的 PHP 为您编写 JS,如下所示:

<?php
echo "function drawChart() {
      var data = google.visualization.arrayToDataTable([
      ['Month', '# of Logins'],
      ['Jan',  " . $jan . " ],
      ['Feb',  " . $feb . "],
      ['Mar',  " . $mar . "],
      ['Apr',  " . $apr . "],
      ['May',  " . $may . "],
      ['Jun',  " . $jun . "],
      ['Jul',  " . $jul . "],
      ['Aug',  " . $aug . "],
      ['Sept',  " . $sept . "],
      ['Oct',  " . $oct . "],
      ['Nov',  " . $nov . "],
      ['Dec',  " . $dec . "]
      ]);";

但是,即使这样也可以通过循环来简化(或至少清理):

//Declaring variables

$monthly_visitors[0][1]= 'SQL Code for Jan';
$monthly_visitors[1][1]= 'SQL Code for Feb';
$monthly_visitors[2][1]= 'SQL Code for Mar';
$monthly_visitors[3][1]= 'SQL Code for Apr';
$monthly_visitors[4][1]= 'SQL Code for May';
$monthly_visitors[5][1]= 'SQL Code for Jun';
$monthly_visitors[6][1]= 'SQL Code for Jul';
$monthly_visitors[7][1]= 'SQL Code for Aug';
$monthly_visitors[8][1]= 'SQL Code for Sept';
$monthly_visitors[9][1]= 'SQL Code for Oct';
$monthly_visitors[10][1]= 'SQL Code for Nov';
$monthly_visitors[11][1]= 'SQL Code for Dec';

$monthly_visitors[0][0]= 'Jan';
$monthly_visitors[1][0]= 'Feb';
$monthly_visitors[2][0]= 'Mar';
$monthly_visitors[3][0]= 'Apr';
$monthly_visitors[4][0]= 'May';
$monthly_visitors[5][0]= 'Jun';
$monthly_visitors[6][0]= 'Jul';
$monthly_visitors[7][0]= 'Aug';
$monthly_visitors[8][0]= 'Sept';
$monthly_visitors[9][0]= 'Oct';
$monthly_visitors[10][0]= 'Nov';
$monthly_visitors[11][0]= 'Dec';


// Writing the Function
echo "function drawChart() {
      var data = google.visualization.arrayToDataTable([
      ['Month', '# of Logins']";

      for ($i=0;$i<count($monthly_visitors);$i++)
      {
        echo ", ['" . $monthly_visitors[i][0] . "', " . $monthly_visitors[i][1] . "]";
      }
      echo "]);";
于 2013-07-05T00:13:07.307 回答