index.php 842 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. require_once 'session.php';
  3. require_once 'database.php';
  4. require_once 'date.php';
  5. require_once 'twig.php';
  6. // Open database connection
  7. $db = new Database ();
  8. $db->connect ();
  9. // Pagination. What page are we in?
  10. if (isset ($_GET['page']))
  11. {
  12. $page = intval ($_GET['page']);
  13. if ($page < 0)
  14. $page = 0;
  15. } else {
  16. $page = 0;
  17. }
  18. // Retrieve list of posts
  19. if (isset ($_GET['new']))
  20. $posts = $db->get_new_posts ($page);
  21. else
  22. $posts = $db->get_hot_posts ($page);
  23. // Retrieve a list of user votes for the posts
  24. $IDs = array ();
  25. foreach ($posts as $post)
  26. $IDs[] = $post['id'];
  27. $votes = $db->get_posts_votes (implode (',', $IDs), Session::get_userid ());
  28. // Render template
  29. echo $twig->render (
  30. 'index.twig',
  31. array(
  32. 'posts' => $posts,
  33. 'votes' => $votes,
  34. 'page' => $page));