By series of pages I mean having pages that can be navigated to using Previous, Next, or 1,2,3, ... The content being a list of 10 songs, and to see more the user can just click Next. I'm planning to use PHP and MySQL. I need your opinions on how to go about it (Not explicitly asking for code but personal opinions that may be supplemented with code). Thanks!
-
Do you mean something like /page/2... and so on?– VeeeneXSep 14, 2014 at 10:39
-
So, a question about pagination?– StrawberrySep 14, 2014 at 10:54
-
Nowadays I prefer infinite scrolling over pagination.– KarolisSep 14, 2014 at 11:13
-
Oh! So that's what they call it, Pagination. Thanks! @Strawberry– SalimSep 14, 2014 at 12:13
2 Answers
MySQL offers the LIMIT X,Y
keyword which does most of this work already. X
is always the starting position and Y
the amount of rows to select.
If you have, for example, a search form, and the user searches for a song of the genre pop, you could do something like SELECT name, artist, ... FROM songs WHERE genre = 'pop' LIMIT 0,10
. This will return 10 songs of the search result starting at position 0. That would be your page 1. For page 2 you simply run the same query again but with LIMIT 10,10
.
Using this you can create your Previous and Next buttons:
HTML
<a href="search.php?query=pop&page=1">Previous</a>
<a href="search.php?query=pop&page=3">Next</a>
PHP
$page = isset($_GET['page']) ? intval($_GET['page'] : 1;
$start = ($page - 1) * 10;
$query = "SELECT name, artist, ... FROM songs WHERE genre = 'pop' LIMIT $start,10";
-
But he want to do that using Ajax maybe you should add How to change it to ajax way using json for example.– VeeeneXSep 14, 2014 at 10:59
-
The technique is called pagination. Here's a PHP helper class that can help you with pagination:
<?php
// This is a helper class to make paginating
// records easy.
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=20, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
public function offset() {
// Assuming 20 items per page:
// page 1 has an offset of 0 (1-1) * 20
// page 2 has an offset of 20 (2-1) * 20
// in other words, page 2 starts with item 21
return ($this->current_page - 1) * $this->per_page;
}
public function total_pages() {
return ceil($this->total_count/$this->per_page);
}
public function previous_page() {
return $this->current_page - 1;
}
public function next_page() {
return $this->current_page + 1;
}
public function has_previous_page() {
return $this->previous_page() >= 1 ? true : false;
}
public function has_next_page() {
return $this->next_page() <= $this->total_pages() ? true : false;
}
}
?>
USE IN CONSTRUCTING THE SQL
$total_count = $db->get_var( "SELECT COUNT(*) FROM songs" );
$per_page = 10;
$current_page = $page;
$pagination = new Pagination($current_page, $per_page, $total_count);
$all = $db->get_results("SELECT * FROM songs ORDER BY id DESC LIMIT {$per_page} OFFSET {$pagination->offset()}");