From f0e911a79c429bd10908071ff1ebf3ee494054a8 Mon Sep 17 00:00:00 2001 From: zPlus <-> Date: Sat, 19 Mar 2016 08:56:28 +0100 Subject: [PATCH] Add pagination to homepage results --- database.php | 53 ++++++++++++++++++++++++++++++++++++++------- index.php | 24 +++++++++++++++----- template/index.twig | 5 +++++ 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/database.php b/database.php index c12b9e5d..79619677 100644 --- a/database.php +++ b/database.php @@ -4,7 +4,12 @@ require_once 'config.php'; class Database { - protected $database; + + /***** STATIC *****/ + + + // How many items to display in homepage + const HOMEPAGE_RESULTS = 50; protected static function get_random_string ($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyz'; @@ -18,6 +23,13 @@ class Database return $hash_id; } + + /***** INSTANCE *****/ + + + // Reference to a database connection + protected $database; + function __construct () { $this->database = NULL; @@ -27,7 +39,14 @@ class Database { try { - $this->database = new PDO (Config::$DATABASE['dsn'], Config::$DATABASE['username'], Config::$DATABASE['password']); + $this->database = new PDO ( + Config::$DATABASE['dsn'], + Config::$DATABASE['username'], + Config::$DATABASE['password']); + + $this->database->setAttribute ( + PDO::ATTR_ERRMODE, + PDO::ERRMODE_EXCEPTION); return true; @@ -317,19 +336,28 @@ class Database /** * Get posts by rating (for homepage) */ - function get_hot_posts () + function get_hot_posts ($page = 0) { $submissions = array(); + $page = intval ($page); if (is_null ($this->database)) return $submissions; - $query = $this->database->query ( + if ($page < 0) + $page = 0; + + $query = $this->database->prepare ( 'SELECT P.*, U.`username`' . 'FROM `post` AS P ' . 'JOIN `user` AS U ON P.`userId` = U.`id`' . 'ORDER BY P.`dateCreated` DESC, P.`vote` DESC, P.`commentsCount` DESC ' . - 'LIMIT 50'); + 'LIMIT :limit OFFSET :offset'); + + $query->bindValue (':limit', Database::HOMEPAGE_RESULTS, PDO::PARAM_INT); + $query->bindValue (':offset', $page * Database::HOMEPAGE_RESULTS, PDO::PARAM_INT); + + $query->execute (); $submissions = $query->fetchAll (PDO::FETCH_ASSOC); @@ -339,19 +367,28 @@ class Database /** * Get posts by date (for homepage) */ - function get_new_posts () + function get_new_posts ($page = 0) { $submissions = array(); + $page = intval ($page); if (is_null ($this->database)) return $submissions; - $query = $this->database->query( + if ($page < 0) + $page = 0; + + $query = $this->database->prepare ( 'SELECT P.*, U.`username`' . 'FROM `post` AS P ' . 'JOIN `user` AS U ON P.`userId` = U.`id`' . 'ORDER BY P.`created` DESC ' . - 'LIMIT 50'); + 'LIMIT :limit OFFSET :offset'); + + $query->bindValue (':limit', Database::HOMEPAGE_RESULTS, PDO::PARAM_INT); + $query->bindValue (':offset', $page * Database::HOMEPAGE_RESULTS, PDO::PARAM_INT); + + $query->execute (); $submissions = $query->fetchAll (PDO::FETCH_ASSOC); diff --git a/index.php b/index.php index 4265120d..ee353143 100644 --- a/index.php +++ b/index.php @@ -6,17 +6,28 @@ require_once 'date.php'; require_once 'twig.php'; // Open database connection -$db = new Database(); -$db->connect(); +$db = new Database (); +$db->connect (); + +// Pagination. What page are we in? +if (isset ($_GET['page'])) +{ + $page = intval ($_GET['page']); + + if ($page < 0) + $page = 0; +} else { + $page = 0; +} // Retrieve list of posts if (isset ($_GET['new'])) - $posts = $db->get_new_posts(); + $posts = $db->get_new_posts ($page); else - $posts = $db->get_hot_posts(); + $posts = $db->get_hot_posts ($page); // Retrieve a list of user votes for the posts -$IDs = array(); +$IDs = array (); foreach ($posts as $post) $IDs[] = $post['id']; @@ -28,4 +39,5 @@ echo $twig->render ( 'index.twig', array( 'posts' => $posts, - 'votes' => $votes)); \ No newline at end of file + 'votes' => $votes, + 'page' => $page)); \ No newline at end of file diff --git a/template/index.twig b/template/index.twig index 82ee1759..e2b0f3e7 100644 --- a/template/index.twig +++ b/template/index.twig @@ -37,6 +37,11 @@ {% endfor %} +