Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Array having the MONTHS as keys (numeric month, from 1 to 12) and the view count as values */
- $months = array();
- /* Initialize the days keys to 0, for both ads and phone */
- for ($i = 0; $i < 12; $i++)
- {
- $monthUts = mktime(0, 0, 0, date('n') - $i, 1, date('Y'));
- $month = intval(date('m', $monthUts), 10);
- $months[$month] = array('ads' => 0, 'phone' => 0);
- }
- /* Now read the view count from the DB. I take the last 12 months. */
- $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")';
- /* Note: be sure to validate $_SESSION['id'] */
- $res = $pdo->prepare($query);
- $res->bindValue(':id', $_SESSION['id'], PDO::PARAM_INT);
- $res->execute();
- /* Now we update the $months array with the view count from the query */
- while ($row = $res->fetch(PDO::FETCH_ASSOC))
- {
- $row_month = intval($row['time'], 10);
- $ads_count = intval($row['view_count_ads'], 10);
- $phone_count = intval($row['view_count_phone'], 10);
- /* If view_count_ads is -1 it means this is a result from phone_vew_count */
- if (ads_count == -1)
- {
- $months[$row_month]['phone'] = $phone_count;
- }
- else /* Otherwise, it's a result from ads_vew_count */
- {
- $months[$row_month]['ads'] = $ads_count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement