<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>paging &#8211; Blackbams Blog</title>
	<atom:link href="https://blog.blackbam.at/tag/paging/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.blackbam.at</link>
	<description>development - digital arts - internet</description>
	<lastBuildDate>Wed, 05 Oct 2011 17:43:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>Implementing a simple and flexible paging algorithm using PHP</title>
		<link>https://blog.blackbam.at/2011/07/18/implementing-a-simple-and-flexible-paging-algorithm-using-php/</link>
					<comments>https://blog.blackbam.at/2011/07/18/implementing-a-simple-and-flexible-paging-algorithm-using-php/#respond</comments>
		
		<dc:creator><![CDATA[Blackbam]]></dc:creator>
		<pubDate>Mon, 18 Jul 2011 08:00:47 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[paging]]></category>
		<category><![CDATA[Tutorial]]></category>
		<guid isPermaLink="false">https://blog.blackbam.at/?p=1403</guid>

					<description><![CDATA[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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>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.</p>
<h2>Case example</h2>
<p>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:<br />
1. The maximum number of articles on an overview page is 10<br />
2. A navigation to navigate to the next and previous 10 articles is required<br />
The result will be, that you need to have four pages:<br />
page 1: article 1-10,<br />
page 2: article 11-20,<br />
page 3: article 21-30,<br />
page 4: article 30-37<br />
<br />
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). <br />
But how can this result always be calculated dynamically?</p>
<h2>The script</h2>
<p>The following script shows the basic implementation of the page results algorithm in PHP:</p>
<pre lang="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);
</pre>
<p><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.blackbam.at%2F2011%2F07%2F18%2Fimplementing-a-simple-and-flexible-paging-algorithm-using-php%2F&#038;title=Implementing%20a%20simple%20and%20flexible%20paging%20algorithm%20using%20PHP" data-a2a-url="https://blog.blackbam.at/2011/07/18/implementing-a-simple-and-flexible-paging-algorithm-using-php/" data-a2a-title="Implementing a simple and flexible paging algorithm using PHP"><img src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.blackbam.at/2011/07/18/implementing-a-simple-and-flexible-paging-algorithm-using-php/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
