home » zplus/freepost.git
Author zPlus <-> 2016-03-19 07:56:28
Committer zPlus <-> 2016-03-19 07:56:28
Commit f0e911a (patch)
Tree af02d19
Parent(s)

Add pagination to homepage results


commits diff: dc75391..f0e911a
3 files changed, 68 insertions, 14 deletionsdownload


Diffstat
-rw-r--r-- database.php 53
-rw-r--r-- index.php 24
-rw-r--r-- template/index.twig 5

Diff options
View
Side
Whitespace
Context lines
Inter-hunk lines
+45/-8 M   database.php
index c12b9e5..7961967
old size: 38K - new size: 39K
@@ -4,7 +4,12 @@ require_once 'config.php';
4 4
5 5 class Database
6 6 {
7 - protected $database;
7 +
8 + /***** STATIC *****/
9 +
10 +
11 + // How many items to display in homepage
12 + const HOMEPAGE_RESULTS = 50;
8 13
9 14 protected static function get_random_string ($length = 10) {
10 15 $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
@@ -18,6 +23,13 @@ class Database
18 23 return $hash_id;
19 24 }
20 25
26 +
27 + /***** INSTANCE *****/
28 +
29 +
30 + // Reference to a database connection
31 + protected $database;
32 +
21 33 function __construct ()
22 34 {
23 35 $this->database = NULL;
@@ -27,7 +39,14 @@ class Database
27 39 {
28 40 try {
29 41
30 - $this->database = new PDO (Config::$DATABASE['dsn'], Config::$DATABASE['username'], Config::$DATABASE['password']);
42 + $this->database = new PDO (
43 + Config::$DATABASE['dsn'],
44 + Config::$DATABASE['username'],
45 + Config::$DATABASE['password']);
46 +
47 + $this->database->setAttribute (
48 + PDO::ATTR_ERRMODE,
49 + PDO::ERRMODE_EXCEPTION);
31 50
32 51 return true;
33 52
@@ -317,19 +336,28 @@ class Database
317 336 /**
318 337 * Get posts by rating (for homepage)
319 338 */
320 - function get_hot_posts ()
339 + function get_hot_posts ($page = 0)
321 340 {
322 341 $submissions = array();
342 + $page = intval ($page);
323 343
324 344 if (is_null ($this->database))
325 345 return $submissions;
326 346
327 - $query = $this->database->query (
347 + if ($page < 0)
348 + $page = 0;
349 +
350 + $query = $this->database->prepare (
328 351 'SELECT P.*, U.`username`' .
329 352 'FROM `post` AS P ' .
330 353 'JOIN `user` AS U ON P.`userId` = U.`id`' .
331 354 'ORDER BY P.`dateCreated` DESC, P.`vote` DESC, P.`commentsCount` DESC ' .
332 - 'LIMIT 50');
355 + 'LIMIT :limit OFFSET :offset');
356 +
357 + $query->bindValue (':limit', Database::HOMEPAGE_RESULTS, PDO::PARAM_INT);
358 + $query->bindValue (':offset', $page * Database::HOMEPAGE_RESULTS, PDO::PARAM_INT);
359 +
360 + $query->execute ();
333 361
334 362 $submissions = $query->fetchAll (PDO::FETCH_ASSOC);
335 363
@@ -339,19 +367,28 @@ class Database
339 367 /**
340 368 * Get posts by date (for homepage)
341 369 */
342 - function get_new_posts ()
370 + function get_new_posts ($page = 0)
343 371 {
344 372 $submissions = array();
373 + $page = intval ($page);
345 374
346 375 if (is_null ($this->database))
347 376 return $submissions;
348 377
349 - $query = $this->database->query(
378 + if ($page < 0)
379 + $page = 0;
380 +
381 + $query = $this->database->prepare (
350 382 'SELECT P.*, U.`username`' .
351 383 'FROM `post` AS P ' .
352 384 'JOIN `user` AS U ON P.`userId` = U.`id`' .
353 385 'ORDER BY P.`created` DESC ' .
354 - 'LIMIT 50');
386 + 'LIMIT :limit OFFSET :offset');
387 +
388 + $query->bindValue (':limit', Database::HOMEPAGE_RESULTS, PDO::PARAM_INT);
389 + $query->bindValue (':offset', $page * Database::HOMEPAGE_RESULTS, PDO::PARAM_INT);
390 +
391 + $query->execute ();
355 392
356 393 $submissions = $query->fetchAll (PDO::FETCH_ASSOC);
357 394

+18/-6 M   index.php
index 4265120..ee35314
old size: 629B - new size: 841B
@@ -6,17 +6,28 @@ require_once 'date.php';
6 6 require_once 'twig.php';
7 7
8 8 // Open database connection
9 - $db = new Database();
10 - $db->connect();
9 + $db = new Database ();
10 + $db->connect ();
11 +
12 + // Pagination. What page are we in?
13 + if (isset ($_GET['page']))
14 + {
15 + $page = intval ($_GET['page']);
16 +
17 + if ($page < 0)
18 + $page = 0;
19 + } else {
20 + $page = 0;
21 + }
11 22
12 23 // Retrieve list of posts
13 24 if (isset ($_GET['new']))
14 - $posts = $db->get_new_posts();
25 + $posts = $db->get_new_posts ($page);
15 26 else
16 - $posts = $db->get_hot_posts();
27 + $posts = $db->get_hot_posts ($page);
17 28
18 29 // Retrieve a list of user votes for the posts
19 - $IDs = array();
30 + $IDs = array ();
20 31
21 32 foreach ($posts as $post)
22 33 $IDs[] = $post['id'];
@@ -28,4 +39,5 @@ echo $twig->render (
28 39 'index.twig',
29 40 array(
30 41 'posts' => $posts,
31 - 'votes' => $votes));
31 > \ No newline at end of file
42 + 'votes' => $votes,
43 + 'page' => $page));
43 < \ No newline at end of file

+5/-0 M   template/index.twig
index 82ee175..e2b0f3e
old size: 1K - new size: 2K
@@ -37,6 +37,11 @@
37 37
38 38 {% endfor %}
39 39
40 + <div class="more">
41 + <a href="?page={{ page + 1 }}" class="btn btn-sm btn-danger">
42 + More <span style="margin-left: 1em">▸</span>
43 + </a>
44 + </div>
40 45 </div>
41 46
42 47 {% include 'footer.twig' %}