Advertisement
AlexWebDevelop

Untitled

Aug 12th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. <?php
  2.  
  3. /* Array having the days as keys (YYYY-mm-dd) and the view count as values */
  4. $days = array();
  5.  
  6. /* Initialize the days keys to 0 */
  7. for ($i = 0; $i < 7; $i++)
  8. {
  9.     $dayUts = mktime(0, 0, 0, date('n'), date('j') - $i, date('Y'));
  10.     $day = date('Y-m-d', $dayUts);
  11.     $days[$day] = 0;
  12. }
  13.  
  14. /* Now read the view count from the DB */
  15. $query = 'SELECT DATE_FORMAT(view_count_date, "%Y-%m-%d") AS time, count(*) AS view_count from ads_view_count WHERE view_count_date >= (NOW() - INTERVAL 7 DAY) AND view_count_user_id = :id';
  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 */
  23. while ($row = $res->fetch(PDO::FETCH_ASSOC))
  24. {
  25.     if (array_key_exists($row['time'], $days))
  26.     {
  27.         $days[$row['time']] = intval($row['view_count'], 10);
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement