<?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>DOC776.org &#187; pagination</title>
	<atom:link href="http://www.doc776.org/tag/pagination/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.doc776.org</link>
	<description>This is the era of information.</description>
	<lastBuildDate>Thu, 16 Jul 2009 17:29:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Pagination Simple Mechanics</title>
		<link>http://www.doc776.org/2009/04/php-pagination-simple-mechanics/</link>
		<comments>http://www.doc776.org/2009/04/php-pagination-simple-mechanics/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 00:49:48 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[structure]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=67</guid>
		<description><![CDATA[It is actually very simple. All you need to know is what page you are on and how many items there is to list in total. Here is an out line:
Step one, get those values:
total_items
items_per_page
current_page
Step two, calculate total pages:
total_pages = ( total_items / items_per_page ) 
Step three, calculate limit offset for database query:
start_item = current_page [...]]]></description>
			<content:encoded><![CDATA[<p>It is actually very simple. All you need to know is what page you are on and how many items there is to list in total. Here is an out line:</p>
<p>Step one, get those values:<br />
total_items<br />
items_per_page<br />
current_page</p>
<p>Step two, calculate total pages:<br />
total_pages = ( total_items / items_per_page ) </p>
<p>Step three, calculate limit offset for database query:<br />
start_item = current_page * items_per_page<br />
end_item = if on last page then ( total_pages &#8211; start_item ) else ( items_per_page )  &#8220;asuming your database works like mysql&#8221;</p>
<p>Be mindful that arrays and databases start from 0 not from 1.</p>
<p>And from there just make your PHP code and add on nifty features of your own.</p>
<p>Check out my previous guide for a nice pagination script with demo.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2009/04/php-pagination-simple-mechanics/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Pagination Guide</title>
		<link>http://www.doc776.org/2009/01/php-pagination-and-sorting/</link>
		<comments>http://www.doc776.org/2009/01/php-pagination-and-sorting/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 19:34:31 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.doc776.org/?p=32</guid>
		<description><![CDATA[At some point I needed to paginate my results of a large database for a more friendly view. Now I use PHP so this tutorial will fully explain how to paginate using php and SQL database, or just an array with all the data. That way you can use the technique with any other database [...]]]></description>
			<content:encoded><![CDATA[<p>At some point I needed to paginate my results of a large database for a more friendly view. Now I use PHP so this tutorial will fully explain how to paginate using php and SQL database, or just an array with all the data. That way you can use the technique with any other database you like. I will go over the basic mechanics and the add nice things on top like pagination and sorting at the same time and how to build a wicked page navigation for the listing. And also I shall show you how to make  a so called server side pagination/sorting using Ajax JavaScript at the end.<span id="more-32"></span></p>
<p><strong>Basic Pagination</strong></p>
<p>For total pages, get total amount of rows in your list and divide it by how many rows you want per page and then ceil() the outcome for a nice rounded answer. The names of variables should be straight forward.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//get our data from a database</span>
<span style="color: #000088;">$total_rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select null from `table`&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//find total pages</span>
<span style="color: #000088;">$total_pages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_rows</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Ceil() rounds the number up in case it is 4.5 so that means the last page will have maybe 10 rows of items left of the 20 per page.</p>
<p>Now  you are all set. Get the current page that the user is viewing or set it to 1 by default.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//if empty, set to 1 if not use what is available</span>
<span style="color: #000088;">$current_page</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$current_page</span><span style="color: #339933;">--;</span> <span style="color: #666666; font-style: italic;">//zero is number! read farther...</span></pre></div></div>

<p>Now you are all set to contract the navigation and the listing.</p>
<p>One more thing you should watch out for is the offset, since in arrays and databases it starts from 0 and the human interface should always start from 1. The code will fully be done in the favor of stating at 0 so there is less complication; it is up to you basicaly if you understand how it works.</p>
<p><strong>Navigation</strong></p>
<p>Best for navigation is to make it as a function, that way you can easily stick it in multiple places by just calling it, and it also makes your code more clean.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//make the function, it will need total pages and current page to work properly</span>
<span style="color: #000000; font-weight: bold;">function</span> pagination_nav<span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_pages</span><span style="color: #339933;">,</span> <span style="color: #000088;">$current_page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//set the empty var, so then we can just add to it like $nav .= 'something'</span>
	<span style="color: #000088;">$nav</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//(optional) this will display a [Prev] button that will flip one page backwards, if already on first page it will show a null link instead.</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//if we can go back wards show link</span>
		<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;listing.php?page='</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;[Prev]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">//if not show null link or delete this whole line all togeather</span>
		<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;[Prev]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//the main part of the nav</span>
	<span style="color: #666666; font-style: italic;">//loop throught all the posible pages from 1 too total ammount</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #000088;">$total_pages</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//if on current page display null link or none at all or make it more fency</span>
			<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;[-'</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'-]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">//if not on the current page then display it as a regular link to that page</span>
			<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;listing.php?page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;['</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #339933;">.</span><span style="color: #0000ff;">']&lt;/a&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//(optional) this will display a [Next] button that will flip one page forward, if already on last page it will show a null link instead.</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #000088;">$total_pages</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;listing.php?page='</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;&gt;[Next]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #666666; font-style: italic;">//if not show null link or delete this whole line all togeather</span>
		<span style="color: #000088;">$nav</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;a href=&quot;#&quot;&gt;[Next]&lt;/a&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//give back the goodness</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$nav</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//to use it</span>
<span style="color: #b1b100;">echo</span> apps_page_nav<span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_pages</span><span style="color: #339933;">,</span> <span style="color: #000088;">$current_page</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//notice we have +1, that's because the function is doing this for the human interface, but total pages does not aply to the rule here as it is not used directly with data array or database.</span></pre></div></div>

<p>The whole process is straight forward, the optional buttons can be manipulated and you can add a button to skip to last page and to first page also.</p>
<p><strong>Listing</strong></p>
<p>This part is more simple then the rest. Now we have some useful variable set and navigation out of the way. Do a quic validation that the current page selected is in between zero or total pages amount or equal to zero or total.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//validate the page number is with in range</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pageID</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$pageID</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #000088;">$total_pages</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//calculate the rows offset</span>
	<span style="color: #666666; font-style: italic;">//the start will be the page number * number of rows per page</span>
	<span style="color: #000088;">$limit_start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current_page</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// * here there is a special case for the last page calculation.</span>
	<span style="color: #666666; font-style: italic;">//If on last page then (total rows - the starting rows) else its 20 per page</span>
	<span style="color: #000088;">$limit_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_page</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$total_pages</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_rows</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$limit_start</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span></pre></div></div>

<p>* For the last page: If there is only like 5 items on the last page then the $limit_end cannot be 20, it will have to be 4. With mysql query it does not matter but in some cases it will matter a lot, if you get data from array for instance. Different databases may have a different syntax for limiting the results.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//get rows of items for that specific page, notice the 'limit'</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;select * from `table` limit <span style="color: #006699; font-weight: bold;">{$limit_start}</span>, <span style="color: #006699; font-weight: bold;">{$limit_end}</span> &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All straight forward. Different databases may have a slightly different syntax. Here if we are on page 1 then it will be limit 0, 20. (the second # is actualy teh ammount of rows starting from the first #) Straight forward.</p>
<p><strong>Displaying</strong></p>
<p>Nothing complicated, you may want to use a table for displaying or a list I will just spit out the results with a &lt;br /&gt; at the end of each line.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//You can use for loop or while loop, with for loop you can make every other row diff color easily.</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'.$row['</span>id<span style="color: #0000ff;">'].'</span> <span style="color: #0000ff;">'.$row['</span>field2<span style="color: #0000ff;">'].'</span> <span style="color: #0000ff;">'.$row['</span>field3<span style="color: #0000ff;">'].'</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>br <span style="color: #339933;">/&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #0000ff;">';
}</span></pre></div></div>

<p>That concluded the basics of pagination.</p>
<p><strong>Ordering and Sorting</strong></p>
<p>Sorting the results is easiest using your database. For mysql you can add &#8220;order by `field` asc&#8221; before giving the limit. To keep pagination and order variables in the URL you will need to add the current ordering to each URL that is generated by the pagination function. Also for the order by option links that you may add you will need to keep the current page teh user is on in the URL so it does not reset to 1.</p>
<p><strong>Ajax / JavaScript</strong></p>
<p>Using JavaScript to make a server side paginating server side table is easy. All you need is a good JavaScript that allows you to load content into a DIV element of the HTML and you are set to go. make a main page like index.html and make pages.php. Index.html will have the actual JavaScript and the DIV element. The pages.php will be the actual pagination and ordering script that will point all links to the JavaScript that will load the content in the DIV. That is server side ordering and pagination, the index stays intact while you can list thought any page and so on.</p>
<p>Ups and downs: Well so much for people who&#8217;s browsers cannot use JavaScript but that&#8217;s a very small fraction. Will save bandwidth, you only show them what they need. If you use server side you make less stress on the user but if you let the client&#8217;s browser do all the work it will put more stress on them (thought around 1000 results wont matter much depending on the size).</p>
<p>There is an example given at the bottom of the tutorial.</p>
<p><strong>Examples:</strong></p>
<p>I have went through this very carefully so that any beginner at PHP may understand this, or so I hope. Remember php.net is your friend.</p>
<p>Simple Pagination: <a title="Demo" href="http://www.doc776.org/examples/paginate.php" target="prevframe">[demo]</a> <a title="Source" href="http://www.doc776.org/examples/syntax_hilight.php?file=paginate.php" target="prevframe">[source]</a></p>
<p>Preview Frame:<br />
<iframe name="prevframe" src ="http://www.doc776.org/examples/blank.htm" width="100%" height="300px">Your browser does not support iframes.</iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.doc776.org/2009/01/php-pagination-and-sorting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
