PHP用Google图表制作图表

时间:2012-12-27 05:37:48

标签: php google-visualization

我正在尝试为我的网站每天获得的观看次数创建Google line chart。我不太清楚如何做到这一点。

这是使一切正常的代码:

<html>
<head>
<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([
      ['Year', 'Sales', 'Expenses'],
      ['2004',  1000,      400],
      ['2005',  1170,      460],
      ['2006',  660,       1120],
      ['2007',  1030,      5412]
    ]);

    var options = {
      title: 'Company Performance'
    };

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

          

现在我想将此更改为列出每天访问我网站的总观看次数,然后将其与每天访问我网站的唯一身份数进行比较。

查询看起来像:

$total = mysql_query("SELECT id, userID, ip, date FROM views WHERE userID!='1'");
$unique = mysql_query("SELECT id, userID, ip, date FROM views GROUP BY ip");

1 个答案:

答案 0 :(得分:0)

总数是错误的,为了得到每天你应该做的事情

SELECT count(*) as nr, date FROM views WHERE userID!='1' GROUP BY date

您的独特访客并非易于计算。我会创建一个在我的查询中使用的视图, 视图将类似于

SELECT count(*) as nr, data, ip FROM views WHERE userID!='1' GROUP BY date, ip ORDER BY date

让我们说你给上面的名称unique_views。实际上并没有使用nr列,但是对其他东西来说很好。

要获得您应该做的唯一访问者

SELECT count(*), date FROM unique_views GROUP BY date ORDER BY date

上述两个查询都应该具有相同数量的记录,因为它们都基于同一个表,所以如果你同时运行它们,你应该能够

$total_per_day = mysql_query("SELECT count(*) as nr, data, ip FROM views WHERE userID!='1' GROUP BY date, ip ORDER BY date");
$unique_per_day = mysql_query("SELECT count(*) as nr, date FROM unique_views WHERE userID!='1' GROUP BY date ORDER BY date");

然后你可以做一个

for($i=0;$i<count($total_per_day);$i++) {
   $result[] = "['" . $total_per_day[$i]["date"] . "'," . $total_per_day[$i]["nr"] . "," . $unique_per_day[$i]["nr"] . "]"
}
$str = implode(",", $result);