1

我正在尝试为我的网站每天获得的浏览量创建一个谷歌折线图。我不太确定该怎么做。

这是使一切正常的代码:

<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");
4

1 回答 1

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);
于 2012-12-27T05:59:53.133 回答