SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | - | /* Array having the days as keys (YYYY-mm-dd) and the view count as values */ |
3 | + | /* Array having the MONTHS as keys (numeric month, from 1 to 12) and the view count as values */ |
4 | - | $days = array(); |
4 | + | $months = array(); |
5 | ||
6 | /* Initialize the days keys to 0, for both ads and phone */ | |
7 | - | for ($i = 0; $i < 7; $i++) |
7 | + | for ($i = 0; $i < 12; $i++) |
8 | { | |
9 | - | $dayUts = mktime(0, 0, 0, date('n'), date('j') - $i, date('Y')); |
9 | + | $monthUts = mktime(0, 0, 0, date('n') - $i, 1, date('Y')); |
10 | - | $day = date('Y-m-d', $dayUts); |
10 | + | $month = intval(date('m', $monthUts), 10); |
11 | - | $days[$day] = array('ads' => 0, 'phone' => 0); |
11 | + | $months[$month] = array('ads' => 0, 'phone' => 0); |
12 | } | |
13 | ||
14 | - | /* Now read the view count from the DB */ |
14 | + | /* Now read the view count from the DB. I take the last 12 months. */ |
15 | - | $query = 'SELECT DATE_FORMAT(view_count_date, "%Y-%m-%d") AS time, count(*) AS view_count_ads, -1 AS view_count_phone FROM ads_view_count WHERE view_count_date >= (NOW() - INTERVAL 7 DAY) AND view_count_user_id = :id GROUP BY DATE_FORMAT(view_count_date, "%Y-%m-%d") UNION SELECT DATE_FORMAT(view_count_date, "%Y-%m-%d") AS time, -1 AS view_count_ads, count(*) AS view_count_phone FROM phone_view_count WHERE view_count_date >= (NOW() - INTERVAL 7 DAY) AND view_count_user_id = :id GROUP BY DATE_FORMAT(view_count_date, "%Y-%m-%d")'; |
15 | + | $query = 'SELECT DATE_FORMAT(view_count_date, "%m") AS time, count(*) AS view_count_ads, -1 AS view_count_phone FROM ads_view_count WHERE view_count_date >= (NOW() - INTERVAL 12 MONTH) AND view_count_user_id = :id GROUP BY DATE_FORMAT(view_count_date, "%m") UNION SELECT DATE_FORMAT(view_count_date, "%m") AS time, -1 AS view_count_ads, count(*) AS view_count_phone FROM phone_view_count WHERE view_count_date >= (NOW() - INTERVAL 12 MONTH) AND view_count_user_id = :id GROUP BY DATE_FORMAT(view_count_date, "%m")'; |
16 | ||
17 | /* Note: be sure to validate $_SESSION['id'] */ | |
18 | $res = $pdo->prepare($query); | |
19 | $res->bindValue(':id', $_SESSION['id'], PDO::PARAM_INT); | |
20 | $res->execute(); | |
21 | ||
22 | - | /* Now we update the $days array with the view count from the query */ |
22 | + | /* Now we update the $months array with the view count from the query */ |
23 | while ($row = $res->fetch(PDO::FETCH_ASSOC)) | |
24 | { | |
25 | - | if (array_key_exists($row['time'], $days)) |
25 | + | $row_month = intval($row['time'], 10); |
26 | - | { |
26 | + | $ads_count = intval($row['view_count_ads'], 10); |
27 | - | $ads_count = intval($row['view_count_ads'], 10); |
27 | + | $phone_count = intval($row['view_count_phone'], 10); |
28 | - | $phone_count = intval($row['view_count_phone'], 10); |
28 | + | |
29 | - | |
29 | + | /* If view_count_ads is -1 it means this is a result from phone_vew_count */ |
30 | - | /* If view_count_ads is -1 it means this is a result from phone_vew_count */ |
30 | + | if (ads_count == -1) |
31 | - | if (ads_count == -1) |
31 | + | { |
32 | - | { |
32 | + | $months[$row_month]['phone'] = $phone_count; |
33 | - | $days[$row['time']]['phone'] = $phone_count; |
33 | + | } |
34 | - | } |
34 | + | else /* Otherwise, it's a result from ads_vew_count */ |
35 | - | else /* Otherwise, it's a result from ads_vew_count */ |
35 | + | { |
36 | - | { |
36 | + | $months[$row_month]['ads'] = $ads_count; |
37 | - | $days[$row['time']]['ads'] = $ads_count; |
37 | + | } |
38 | - | } |
38 | + |