Warning: Parameter 2 to qtranxf_postsFilter() expected to be a reference, value given in /home/.sites/609/site1266/web/blackbams-blog/wp-includes/class-wp-hook.php on line 286 Warning: count(): Parameter must be an array or an object that implements Countable in /home/.sites/609/site1266/web/blackbams-blog/wp-includes/post-template.php on line 284 Implementing a simple and flexible paging algorithm using PHP • algorithm paging Tutorial • Blackbams Blog
18. July 2011
von Blackbam

A very common problem in programming web pages is to implement paging. Paging is required whenever there is an undefined amount of items (usually queried from a database) with a maximum amount of items to be displayed on one page. If there is more than one page, there must be a possibility to navigate through these pages while each page displays the desired amount of items.

Case example

Imagine you have a blog with a dynamic number of articles, while the current number of articles is 37. You want to create an article overview yourself, with the following conditions:
1. The maximum number of articles on an overview page is 10 2. A navigation to navigate to the next and previous 10 articles is required
The result will be, that you need to have four pages: page 1: article 1-10, page 2: article 11-20, page 3: article 21-30, page 4: article 30-37
Knowing the result, it is easy to develop a navigation between these pages: You just need to know the number of articles to show on each page (usually decided when coding the script), the page you are currently on (usually passed via Post- or Get- paramters and the number of all results you have to display (usually from counting the SQL results).
But how can this result always be calculated dynamically?

The script

The following script shows the basic implementation of the page results algorithm in PHP:

/*** The three paramters ***/
// Make a query to get all your results (like posts, images, whatever)
$resultsOverall = 0;
// Results per page (the number of results per page to be shown)
$resultsPerPage = 10;
// Page number (the current page number, usually $_REQUEST['pageNumber'] or something similar)
$pageNumber = 1; 
if(isset($_REQUEST["pageNumber"]) && $_REQUEST["pageNumber"] > 1) {
	$pageNumber = $_REQUEST["pageNumber"];
/**** The logic ****/
// determine the first result to show
$resultsFrom = ($pageNumber*$resultsPerPage-$resultsPerPage+1);
// determine the last result to show
$resultsTo = ($resultsFrom-1)+$resultsPerPage;
if($resultsTo > $resultsOverall) {
	$resultsTo = $resultsOverall;
// determine number of Pages (for example to show in a navigation)
$allPages = ceil($resultsOverall / $resultsPerPage);

Dieser Eintrag wurde am 18. July 2011 um 10:10 in der Kategorie PHP veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.

Tags: , ,

Warning: Use of undefined constant Ext_related_links - assumed 'Ext_related_links' (this will throw an Error in a future version of PHP) in /home/.sites/609/site1266/web/blackbams-blog/wp-content/themes/SilentWoodsByBlackbam/single.php on line 75

No comments yet

Kommentare abonnieren (RSS) or URL Trackback

Leave a comment: