<?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>coffeecoders.de &#187; pagination</title>
	<atom:link href="http://coffeecoders.de/tag/pagination/feed/" rel="self" type="application/rss+xml" />
	<link>http://coffeecoders.de</link>
	<description>professional devs at work</description>
	<lastBuildDate>Tue, 13 Sep 2011 16:34:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Adding a simple pagination to your site</title>
		<link>http://coffeecoders.de/2009/02/adding-a-simple-pagination-to-your-site/</link>
		<comments>http://coffeecoders.de/2009/02/adding-a-simple-pagination-to-your-site/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 02:19:28 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[common]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://coffeecoders.de/?p=184</guid>
		<description><![CDATA[or: the dangerousness of simple mathematics I guess many people had this problem before and I think everybody managed to solve this quite similar. Anyways, just in case this is useful for anybody: here&#8217;s how I added a pagination to &#8230; <a href="http://coffeecoders.de/2009/02/adding-a-simple-pagination-to-your-site/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>or: the dangerousness of simple mathematics</h3>
<p>I guess many people had this problem before and I think everybody managed to solve this quite similar. Anyways, just in case this is useful for anybody: here&#8217;s how I added a pagination to a site recently.</p>
<p>Given you have an array of products for example. Or maybe search results, a list of articles&#8230; something like that. Unfortunately they are too many to display them on a single page. Well, sure, you could but let&#8217;s be honest: that isn&#8217;t an option as the page would exceed its &#8220;scrollable-with-no-harm-to-scroll-fingers&#8221; length. And you don&#8217;t want to get sued by your visitors, right <img src='http://coffeecoders.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
Ok, so you&#8217;re going to add a nice pagination to your site, so that everybody is happy.<br />
What I found out to be a bit tricky was to calculate the actual number of pages. We need that number so that we know, how many page links we will show next to our current view.<br />
<span id="more-184"></span><br />
Two simple examples to illustrate the probelm:<br />
Let&#8217;s say we have <strong>8</strong> items. <strong>8 divided by 4 is 2</strong>, so we&#8217;re going to need two pages. No big deal, right?<br />
Okay then we try this with <strong>9</strong> items. <strong>9 divided by 4 gives us 2.25</strong>. So we will clearly need a 3rd page for product number 9, but how do we calculate this from our result?<br />
<code><br />
$itemcount = $res-&gt;length();<br />
$pagemin = $itemcount / 4;<br />
$pagemax = ceil($itemcount / 4);<br />
if ( $pagemax - intval($pagemin) === 1) ) {<br />
$pages = $pagemax;<br />
} else {<br />
$pages = intval($pagemin);<br />
}<br />
</code></p>
<h3>The code explained</h3>
<p>That&#8217;s why I compare two values here: the actual division result and the next bigger integer. Now we try to find out if both values differ. If they do, we have a scenario where we do want another page, so we pick the bigger result as our page number. If not we are just happy and use the smaller number.</p>
<p>So now that we know our actual page count we can do something (slightly simplified) like this:</p>
<p><code><br />
while($i = 0;$i &lt; $pages; $i++) {<br />
print '&lt;a href="?page=' . $i . '"&gt;Page ' . $i . '';<br />
}<br />
</code></p>
<figure id="attachment_192" aria-labelledby="figcaption_attachment_192" class="wp-caption aligncenter" style="width: 499px"><img class="size-full wp-image-192" src="http://coffeecoders.de/wp-content/uploads/2009/02/pagination.jpg" alt="A pagination helps to navigate through long lists of items" width="489" height="134" /><figcaption id="figcaption_attachment_192" class="wp-caption-text">A pagination helps to navigate through long lists of items</figcaption></figure>
<p>With a little bit of styling this even looks nice. And of course, this is just a start. You also might want to add &#8220;previous&#8221; and &#8220;next buttons&#8221;, or those &#8220;first page&#8221;, &#8220;last page&#8221; buttons, limit the maximum number of page links shown, or you could center the currently viewed page in the middle, hide the previous button on the first page, etc&#8230; There are a lot of fine tuning options that will improve the user experience of your pagination.</p>
]]></content:encoded>
			<wfw:commentRss>http://coffeecoders.de/2009/02/adding-a-simple-pagination-to-your-site/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

