如何获取用户最近31天的访问记录
How to get last 31 days Visits record of user
我正在尝试向用户显示本月的个人资料视图,现在如果我从日期的第 1 天显示到今天,我的代码可以完美运行,但是当用户在我的网站上注册时出现问题,让我们假设用户在 3 月 26 日注册
现在我无法显示过去 25 天的 0 个视图,因为数据库中不存在他的数据。让我给你看我的代码
$year = date("Y");
$month = date("m");
$number = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$q = $db->getRows("SELECT date AS dates, COUNT(user_id) AS view
FROM profile_analytics
WHERE user_id = ?
AND date BETWEEN ? AND ?
GROUP BY `date`
ORDER BY `date`",
PDO::FETCH_ASSOC,
[Profile::getid(),
"$year-$month-1",
"$year-$month-$number"]);
$i = 0;
while ($i <= $number) {
$i++;
$a = $i - 1;
if (isset($q[$a]['dates'])) {
if ($q[$a]['dates'] != "$year-$month-$i") {
$click = 0;
} else {
$click = $q[$a]['view'];
}
} else {
$click = 0;
}
$array [] = $click;
}
return implode(', ', $array);
}
这是我从数据库中得到的
Array
(
[0] => Array
(
[dates] => 2017-03-25
[view] => 1
)
[1] => Array
(
[dates] => 2017-03-26
[view] => 15
)
)
毕竟这就是我得到的
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
我知道我的方法不对,但我需要更好的方法
这是我收到的新回复
Array
(
[0] => Array
(
[dates] => 2017-03-01
[view] => 1
)
[1] => Array
(
[dates] => 2017-03-02
[view] => 1
)
[2] => Array
(
[dates] => 2017-03-03
[view] => 1
)
[3] => Array
(
[dates] => 2017-03-25
[view] => 1
)
[4] => Array
(
[dates] => 2017-03-26
[view] => 13
)
)
这是我内爆后得到的
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 13, 0, 0, 0, 0, 0, 1, 1, 1
function xxx()
{
$year = date("Y");
$month = date("m");
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$q = $db->getRows("SELECT date AS dates, COUNT(user_id) AS view
FROM profile_analytics
WHERE user_id = ?
AND date BETWEEN ? AND ?
GROUP BY `date`
ORDER BY `date`",
PDO::FETCH_ASSOC,
[Profile::getid(),
"$year-$month-1",
"$year-$month-$days_in_month"]);
// for each day in the month init the totals array
// one for every day of the month to ZERO
$views = array_fill(1,(int)$days_in_month,0);
// foreach date that someone actually viewed this users profile
// put a real count in the $views array
foreach ( $q as $viewed ) {
$the_day = (int)substr($viewed['dates'], -2); // get the day from the date
$views[$the_day] = $viewed['view'];
}
return implode(', ', $views);
}
如果我们以您所说的从数据库查询返回的格式虚拟一些数据...
function xxx()
{
$year = date("Y");
$month = date("m");
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$q = [
['dates' => '2017-03-01' , 'view' => 1],
['dates' => '2017-03-02' , 'view' => 1],
['dates' => '2017-03-03' , 'view' => 1],
['dates' => '2017-03-10' , 'view' => 1],
['dates' => '2017-03-25' , 'view' => 5],
['dates' => '2017-03-26' , 'view' => 10]
];
// for each day in the month init the totals array
// one for every day of the month to ZERO
$views = array_fill(1,(int)$days_in_month,0);
// foreach date that someone actually viewed this users profile
// put a real count in the $views array
foreach ( $q as $viewed ) {
$the_day = (int)substr($viewed['dates'], -2); // get the day from the date
$views[$the_day] = $viewed['view'];
}
return implode(', ', $views);
}
$s = xxx();
print $s;
输出为
1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 0, 0, 0, 0, 0
我正在尝试向用户显示本月的个人资料视图,现在如果我从日期的第 1 天显示到今天,我的代码可以完美运行,但是当用户在我的网站上注册时出现问题,让我们假设用户在 3 月 26 日注册 现在我无法显示过去 25 天的 0 个视图,因为数据库中不存在他的数据。让我给你看我的代码
$year = date("Y");
$month = date("m");
$number = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$q = $db->getRows("SELECT date AS dates, COUNT(user_id) AS view
FROM profile_analytics
WHERE user_id = ?
AND date BETWEEN ? AND ?
GROUP BY `date`
ORDER BY `date`",
PDO::FETCH_ASSOC,
[Profile::getid(),
"$year-$month-1",
"$year-$month-$number"]);
$i = 0;
while ($i <= $number) {
$i++;
$a = $i - 1;
if (isset($q[$a]['dates'])) {
if ($q[$a]['dates'] != "$year-$month-$i") {
$click = 0;
} else {
$click = $q[$a]['view'];
}
} else {
$click = 0;
}
$array [] = $click;
}
return implode(', ', $array);
}
这是我从数据库中得到的
Array
(
[0] => Array
(
[dates] => 2017-03-25
[view] => 1
)
[1] => Array
(
[dates] => 2017-03-26
[view] => 15
)
)
毕竟这就是我得到的
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
我知道我的方法不对,但我需要更好的方法
这是我收到的新回复
Array
(
[0] => Array
(
[dates] => 2017-03-01
[view] => 1
)
[1] => Array
(
[dates] => 2017-03-02
[view] => 1
)
[2] => Array
(
[dates] => 2017-03-03
[view] => 1
)
[3] => Array
(
[dates] => 2017-03-25
[view] => 1
)
[4] => Array
(
[dates] => 2017-03-26
[view] => 13
)
)
这是我内爆后得到的
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 13, 0, 0, 0, 0, 0, 1, 1, 1
function xxx()
{
$year = date("Y");
$month = date("m");
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$q = $db->getRows("SELECT date AS dates, COUNT(user_id) AS view
FROM profile_analytics
WHERE user_id = ?
AND date BETWEEN ? AND ?
GROUP BY `date`
ORDER BY `date`",
PDO::FETCH_ASSOC,
[Profile::getid(),
"$year-$month-1",
"$year-$month-$days_in_month"]);
// for each day in the month init the totals array
// one for every day of the month to ZERO
$views = array_fill(1,(int)$days_in_month,0);
// foreach date that someone actually viewed this users profile
// put a real count in the $views array
foreach ( $q as $viewed ) {
$the_day = (int)substr($viewed['dates'], -2); // get the day from the date
$views[$the_day] = $viewed['view'];
}
return implode(', ', $views);
}
如果我们以您所说的从数据库查询返回的格式虚拟一些数据...
function xxx()
{
$year = date("Y");
$month = date("m");
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$q = [
['dates' => '2017-03-01' , 'view' => 1],
['dates' => '2017-03-02' , 'view' => 1],
['dates' => '2017-03-03' , 'view' => 1],
['dates' => '2017-03-10' , 'view' => 1],
['dates' => '2017-03-25' , 'view' => 5],
['dates' => '2017-03-26' , 'view' => 10]
];
// for each day in the month init the totals array
// one for every day of the month to ZERO
$views = array_fill(1,(int)$days_in_month,0);
// foreach date that someone actually viewed this users profile
// put a real count in the $views array
foreach ( $q as $viewed ) {
$the_day = (int)substr($viewed['dates'], -2); // get the day from the date
$views[$the_day] = $viewed['view'];
}
return implode(', ', $views);
}
$s = xxx();
print $s;
输出为
1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 10, 0, 0, 0, 0, 0