dongyi7966 2014-03-20 21:08
浏览 50
已采纳

php - 获取过去7天的点击总和

I have a table xeon_users_rented, with: clicks0, clicks1, clicks2, clicks3, clicks4, clicks5, clicks6

Each day, clicks0 will increase, and every day at midnight, a cronjob will run, making clicks0 = clicks1 (setting todays clicks, to yesterday clicks), and then set clicks0 to zero.

What I am trying to achieve is I want to make a graph, that shows the sum of clicks0, clicks1 etc., where clicks0 is todays date.

I have the query below:

$data = array();
for ($x = 0; $x <= 6; $x++) {
    $date = date("Y/m/d", time() - ($x * 86400));
    $queryE = $dbh->prepare("SELECT SUM(clicks$x) FROM xeon_users_rented WHERE user_by=:username");
    $queryE->bindParam(":username", $userdata['username']);
    $queryE->execute();
    $row = $queryE->fetch(PDO::FETCH_ASSOC);
    $dates[] = date("Y/m/d", time() - ($x * 86400));   
    $data[] = ($row['clicks'.$x.''] > 0 ? $row['clicks'.$x.''] : 0);
}
$days = array('Today');
for ($i = 0; $i < 6; $i++) {
    $days[$i] = date('d-m', strtotime('-'.($i + 0).' day'));
}

The $days is working perfectly - it will print out today, and the last couple of days.

The $data is not working. It is just printing out:

0,0,0,0,0,0,0

Can someone please help me out here.

  • 写回答

1条回答 默认 最新

  • dpjw67160 2014-03-20 21:17
    关注

    The column from your SUM isn't going to be named clicks$x. It will be named something like SUM(clicks1).

    Provide an explicit name in the SQL, like

    SELECT SUM(clicks$x) as clickSum ...
    

    Then reference it in row as

    $row['clickSum']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度