dongyi7966 2014-03-20 13: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 13: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']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部