Advertisement
AlexWebDevelop

Untitled

Dec 16th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /* Your own timezone */
  4. $time_zone = 'Europe/Rome';
  5.  
  6. /* Set the PHP time zone */
  7. date_default_timezone_set($time_zone);
  8.  
  9. /* Set the MySQL time zone */
  10. $query = 'SET time_zone = \'' . $time_zone . '\'';
  11. $res = $pdo->prepare($query);
  12. $res->execute();
  13.  
  14. /* Array having the days as keys (YYYY-mm-dd) and the view count as values */
  15. $days = array();
  16.  
  17. /* Initialize the days keys to 0, for both ads and phone */
  18. for ($i = 0; $i < 7; $i++)
  19. {
  20.     $dayUts = mktime(0, 0, 0, date('n'), date('j') - $i, date('Y'));
  21.     $day = date('Y-m-d', $dayUts);
  22.     $days[$day] = array('ads' => 0, 'phone' => 0);
  23. }
  24.  
  25. /* Now read the view count from the DB */
  26. $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 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';
  27.  
  28. /* Note: be sure to validate $_SESSION['id'] */
  29. $res = $pdo->prepare($query);
  30. $res->bindValue(':id', $_SESSION['id'], PDO::PARAM_INT);
  31. $res->execute();
  32.  
  33. /* Now we update the $days array with the view count from the query */
  34. while ($row = $res->fetch(PDO::FETCH_ASSOC))
  35. {
  36.     if (array_key_exists($row['time'], $days))
  37.     {
  38.         $ads_count = intval($row['view_count_ads'], 10);
  39.         $phone_count = intval($row['view_count_phone'], 10);
  40.        
  41.         /* If view_count_ads is -1 it means this is a result from phone_vew_count */
  42.         if (ads_count  == -1)
  43.         {
  44.             $days[$row['time']]['phone'] = $phone_count;
  45.         }
  46.         else /* Otherwise, it's a result from ads_vew_count */
  47.         {
  48.             $days[$row['time']]['ads'] = $ads_count;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement