如何获得最近31天访问用户记录

时间:2017-03-26 15:37:00

标签: php mysql arrays analytics

我正在尝试向用户展示本月的个人资料视图,现在我的代码工作完美,如果我从日期的第一天到今天的日期显示,但是当用户在我的网站上注册时出现问题,让我们假设用户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

1 个答案:

答案 0 :(得分:0)

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