简体   繁体   English

php-获取上周的数据(数量)

[英]php - get data (amount) from the last week

I have a table called "transactions", and in there I input the users ID, purchase type and amount everytime someone makes a purchase on my website. 我有一个名为“交易”的表格,每次有人在我的网站上购物时,我都会在其中输入用户ID,购买类型和金额。

I want to show these stats to each user, for the past 7 days. 我想在过去7天中向每个用户显示这些统计信息。

Currently, I have this: 目前,我有这个:

$data = array();
for($x = 0; $x <= 6; $x++){ 
$time = time()-($x*86400);
$date = date("Y/m/d", time()-($x*86400));
$queryE = $dbh->prepare("SELECT * FROM transactions WHERE user=:username AND time=:time AND(type='upgrade' OR type='addrbl' OR type='clicks' OR type='banner') ");
$queryE->bindParam(":username",$userdata['id']);
$queryE->bindParam(":time",$time);
$queryE->execute();

$row=$queryE->fetch();
$data[] = ($row['amount'] > 0 ? $row['amount'] : 0);
$dates[] = date("Y/m/d", time()-($x*86400));   


}
$days = array('Today');
for ($i=0; $i<7;$i++)
{
  $days[$i] = date('d-m', strtotime('-'.($i+0).' day'));
}

And then to print it, I have this: 然后打印出来,我有这个:

echo implode(',', $data);

Although above code simply prints out 0,0,0,0,0,0,0 尽管上面的代码只是打印出0,0,0,0,0,0,0,0

If I remove the $time from the query, then it only select the first row and print out that for each day. 如果我从查询中删除$time ,那么它只会选择第一行并每天打印出来。

Example: If someone purchased 3 things, but the first thing costed $60, then it will print out 60,60,60,60,60,60,60 示例:如果某人购买了3件商品,但第一件商品的价格为60美元,那么它将打印出60,60,60,60,60,60,60

Can someone please help me how to select the sum of the amount, from each day and print it out? 有人可以帮我从每天中选择金额的总和并打印出来吗?

Edited after learning time is a unix timestamp in db 学习time后编辑的是db中的unix时间戳

Currently you are trying to fetch records for a given time (not date). 当前,您正在尝试获取给定时间(而非日期)的记录。 Since you have a unix timestamp field time on db, you need to give an interval. 由于您在db上具有unix时间戳字段time ,因此需要给出一个间隔。 Also, as there might be multiple purchases on a day, you need to SUM all amounts for a given date. 另外,由于一天可能要进行多次购买,因此您需要将给定日期的所有amounts SUM

...
$begin = strtotime('today midnight');
for($x = 0; $x <= 6; $x++) {
  $end = $begin + 86400;
  $queryE = $dbh->prepare("SELECT SUM(amount) AS total FROM transactions WHERE user=:username AND time > :begin AND time <= :end AND(type='upgrade' OR type='addrbl' OR type='clicks' OR type='banner') ");
  $queryE->bindParam(":username",$userdata['id']);
  $queryE->bindParam(":begin", $begin);
  $queryE->bindParam(":end", $end);
  $queryE->execute();

  $row=$queryE->fetch();
  $data[] = $row['total'];
  $dates[] = date("Y/m/d", $begin);
  $begin -= 86400;
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM