<?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>The Infinite Loop - Beginner&#039;s SEO, Beginner C# &#38; JQuery Tutorials &#187; PHP</title>
	<atom:link href="http://theinfiniteloopblog.com/category/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://theinfiniteloopblog.com</link>
	<description>Problem. Problem Solved. Loop. - The life of a programmer</description>
	<lastBuildDate>Wed, 17 Aug 2011 15:29:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Paging in PHP &#8211; PHP Paging Tutorial</title>
		<link>http://theinfiniteloopblog.com/programming/php/paging-in-php-php-paging-tutorial/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=paging-in-php-php-paging-tutorial</link>
		<comments>http://theinfiniteloopblog.com/programming/php/paging-in-php-php-paging-tutorial/#comments</comments>
		<pubDate>Sun, 30 May 2010 01:56:07 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[beginner css]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css layout]]></category>
		<category><![CDATA[css techniques]]></category>
		<category><![CDATA[css tricks]]></category>
		<category><![CDATA[custom php paging]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[tableless design]]></category>
		<category><![CDATA[tableless layout]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://theinfiniteloopblog.com/?p=381</guid>
		<description><![CDATA[<strong>Paging in php and automatic pagination</strong> is something every coder has to deal with when coding in php. I mean, you COULD show all results on 1 page and skip the php paging, but paging is necessary to reduce bandwidth and organize content. Paging is very easy to do and I will walk you through it step by step. 
]]></description>
			<content:encoded><![CDATA[
<!-- ALL ADSENSE ADS DISABLED -->
<h2 style="font-size:8pt;">PHP Paging &amp; Automatic Pagination Tutorial - Paging made easy</h2>
<p>I have written a <strong>php paging tutorial</strong> before. Admittedly, it wasn't the greatest. I don't think I was as clear as I wanted to be. SO I'm trying again. <img src='http://theinfiniteloopblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  At the end of this tutorial, there is a file named <b>php-automatic-pagination.php.txt</b><br />
Please download it! It will help you understand the following.</p>
<p>Here is what we will be making..nice, simple pagers! (Dont worry about the Image #x text, thats really not part of the tutorial)<br />
<a href="http://theinfiniteloopblog.com/wp-content/uploads/2010/05/php-automatic-pagination.jpg" target="_blank"><img src="http://theinfiniteloopblog.com/wp-content/uploads/2010/05/php-automatic-pagination-300x141.jpg" alt="PHP Automatic Pagination/Simple PHP Paging Tutorial" title="php-automatic-pagination" width="300" height="141" class="alignnone size-medium wp-image-400" /></a></p>
<p><strong>Paging in php and automatic pagination</strong> is something every coder has to deal with when coding in php. I mean, you COULD show all results on 1 page and skip the php paging, but paging is necessary to reduce bandwidth and organize content. Paging is very easy to do and I will walk you through it step by step. </p>
<p>PHP automatic paging comes with a few questions:<br />
Paging 1 : Page Size - How many results would you like to display per page?<br />
Paging 2 : How can we bring back proper results in mysql?<br />
Paging 3 : How can we print a pager that will help show the next items.</p>
<p>Decide on your page size - 10, 15, 25, 50, 100. It really depends on what you would like to do.  For this tutorial I will use 15.</p>
<p class="comment">//we want 15 results per page</p>
<p>$pgsize=15; </p>
<p class="comment">//if there is a p variable in the query string e.g: page.php?p=5 we will get this value, else we will use 1 (page 1)</p>
<p>$pg=(is_numeric($_GET["p"]) ? $_GET["p"] : 1); </p>
<p class="comment">//lets determine where we would like to get results from<br />
//if $pg is 1, this calculation results in 0 (we want to start from the first record in the database)<br />
//if $pg is 2, this calculation will result in 1*15..which means we will start at the 15th index of the database (row 16)</p>
<p>$start=($pg-1)*$pgsize; </p>
<p class="comment">//let us query the database for our results<br />
//you will notice LIMIT and $start and $pgsize.<br />
//On page 1, this query will be SELECT * FROM image_table LIMIT 0,15  - this means return 15 rows starting at row-index 0 (row 1)<br />
//On page 2, this query will be SELECT * FROM image_table LIMIT 15, 15 - this means we will return 15 rows starting at row-index 15 (row 16)<br />
//On page 3, this query will be SELECT * FROM image_table LIMIT 30,15 - this means we will return 15 rows starting at row-index 30 (row 31)<br />
GET IT?!?!?!</p>
<p>$imgs=mysql_query("SELECT * FROM image_table LIMIT $start, $pgsize");</p>
<p class="comment">//we will also need the total number of records in our table<br />
//notice there is no limit? we want to know how many records are in the entire table!<br />
//we can use COUNT(*), COUNT(1), or COUNT(fieldname), they will all return the same result</p>
<p>$img_total=mysql_query("SELECT COUNT(1) FROM image_table");</p>
<p class="comment">//mysql_query returns a resource id, we can use this to get the row it has returned</p>
<p>$img_total=mysql_fetch_row($img_total);</p>
<p class="comment">//the count will be the first field in this row</p>
<p>$img_total=$img_total[0];</p>
<p class="comment">//lets determine how many pages we will need<br />
 //WRONG!, this will cut off results..why?<br />
//say we have 31 images, and our page size is 15. 31/15= ~2.x...We need to round this result up!</p>
<p>$max_pages=$img_total / $pgsize; </p>
<p class="comment">//use this line instead</p>
<p>$max_pages=ceil($img_total/$pgsize);</p>
<p>Once we have all this information, we can use a handy little function I have written named printPager. This function will print out a neat little pager. It uses a helper function named add_querystring_var which was from Added Bytes here:<br />
<a href="http://www.addedbytes.com/code/querystring-functions/">http://www.addedbytes.com/code/querystring-functions/</a></p>
<p>Copy/paste this function into your page or into one of your includes:<br />
This will print out your awesome little pager! <img src='http://theinfiniteloopblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2 style="font-size:1em;">PHP Automatic Pagination Function</h2>
<div style="font-size:0.9em;">
function printPager($max_pages,$pg){<br />
	print '&lt;div class="clear"&gt;&lt;/div&gt;';<br />
	print '&lt;ul class="pager"&gt;';<br />
	$i=1;<br />
	if($pg!=1){<br />
		$url=$_SERVER["SCRIPT_NAME"];<br />
		$url.=add_querystring_var("?".$_SERVER["QUERY_STRING"], "p", $pg-1);<br />
		print '&lt;li class="prev"&gt;&lt;a href="'.$url.'"&gt;&lt;&lt;&lt;/a&gt;&lt;/li&gt;';<br />
	}</p>
<p>	while($i&lt;=$max_pages){<br />
		$url=$_SERVER["SCRIPT_NAME"];<br />
		$url.=add_querystring_var("?".$_SERVER["QUERY_STRING"], "p", $i);</p>
<p>		if($pg==$i){<br />
			print '&lt;li class="current"&gt;';<br />
			print '&lt;span&gt;'.$i.'&lt;/span&gt;';<br />
			print "&lt;/li&gt;";<br />
		}else{<br />
			print "&lt;li&gt;";<br />
			print '&lt;a href="'.$url.'"&gt;'.$i.'&lt;/a&gt;';<br />
			print "&lt;/li&gt;";<br />
		}</p>
<p>		$i++;<br />
	}</p>
<p>	if($pg!=$max_pages){<br />
		$url=$_SERVER["SCRIPT_NAME"];<br />
		$url.=add_querystring_var("?".$_SERVER["QUERY_STRING"], "p", $pg+1);<br />
		print '&lt;li class="next"&gt;&lt;a href="'.$url.'"&gt;&gt;&gt;&lt;/a&gt;';<br />
	}<br />
	print '&lt;/ul&gt;';<br />
	print '&lt;div class="clear"&gt;&lt;/div&gt;';<br />
}</p>
<p>function add_querystring_var($url, $key, $value) { $url = preg_replace('/(.*)(\?|&#038;)' . $key . '=[^&#038;]+?(&#038;)(.*)/i', '$1$2$4', $url . '&#038;'); $url = substr($url, 0, -1); if (strpos($url, '?') === false) { return ($url . '?' . $key . '=' . $value); } else { return ($url . '&#038;' . $key . '=' . $value); } }
</p></div>
<p>Print Pager is called like so:<br />
<?php printPager($max_pages, $pg); ?></p>
<h2 style="font-size:1em;">PHP Pager Styles</h2>
<div style="font-size:0.9em;">
We can style this pager using the following:<br />
/*pagers*/</p>
<p>div.clear{clear:both;}<br />
/spacing between numbers and sizes<br />
ul.pager li {<br />
	float:left;<br />
	margin-right:5px;<br />
	font-size:8pt;<br />
	padding: 2px 5px 2px 5px;<br />
}</p>
<p>//link color<br />
ul.pager li a{<br />
	color:red;<br />
}</p>
<p>//link hover style<br />
ul.pager li a:hover{<br />
	text-decoration:underline;<br />
	color:red;<br />
}</p>
<p>//current page styling<br />
ul.pager li.current span{<br />
	color:red;<br />
}</p>
<p>//previous and next stylings<br />
ul.pager li.prev, ul.pager li.next{</p>
<p>}
</p></div>
<p>Download this tutorials script! It will help you understand. Right click: <a href='http://theinfiniteloopblog.com/wp-content/uploads/2010/05/php-automatic-pagination-tutorial.php.txt'>php-automatic-pagination-tutorial.php</a> and click Save as. Then get rid of the .txt extension and name it php if you would like to run it. You will need to setup a database table to use for the tutorial. It can have an id field and another field.</p>
<p>If you enjoyed this post please leave a comment! I like reading and responding to all my readers!<br />
Thanks</p>
<p><a href="http://secure.hostgator.com/~affiliat/cgi-bin/affiliates/clickthru.cgi?id=aburningflame-"><img src="http://tracking.hostgator.com/img/Green/468x60.gif" border=0></a><br />
<strong>Use HostGator 2010 Coupon Code: <em>994offhgpackage</em> for $9.94 off your first month!<br />
(First month will cost $0.01)</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://theinfiniteloopblog.com/programming/php/paging-in-php-php-paging-tutorial/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Easy, Simple Paging Using PHP</title>
		<link>http://theinfiniteloopblog.com/dbs/easy-simple-paging-using-php/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=easy-simple-paging-using-php</link>
		<comments>http://theinfiniteloopblog.com/dbs/easy-simple-paging-using-php/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 07:10:16 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[custom php paging]]></category>
		<category><![CDATA[easy php paging]]></category>
		<category><![CDATA[how to php paging]]></category>
		<category><![CDATA[php automatic pagination]]></category>
		<category><![CDATA[php automatic paging]]></category>
		<category><![CDATA[php pagination]]></category>
		<category><![CDATA[php paging]]></category>
		<category><![CDATA[php paging tutorial]]></category>
		<category><![CDATA[simple php paging]]></category>

		<guid isPermaLink="false">http://scottmoniz.com/programmingBlog/?p=243</guid>
		<description><![CDATA[Easy &#38; Simple Paging using PHP. Here is the result: (DB has 8records, Rows per page is set to 5, and we're on page 2) Paging usually takes a while but here we will do it quickly and cleanly. Your page will be named page.php. And when we want to switch pages we will make [...]]]></description>
			<content:encoded><![CDATA[
<!-- ALL ADSENSE ADS DISABLED -->
<p style="text-align: center;">Easy &amp; Simple Paging using PHP. Here is the result: (DB has 8records, Rows per page is set to 5, and we're on page 2)<br />
<a href="http://coulditgetworse.com/theinfiniteloopblog/wp-content/uploads/2009/09/advertisers1.jpg"><img class="aligncenter size-full wp-image-252" title="advertisers" src="http://coulditgetworse.com/theinfiniteloopblog/wp-content/uploads/2009/09/advertisers1.jpg" alt="advertisers" width="489" height="128" /></a></p>
<p>Paging usually takes a while but here we will do it quickly and cleanly.</p>
<p>Your page will be named page.php. And when we want to switch pages we will make it page.php?pg=2.<br />
Im sure you've seen this before. Anyways lets dive in.</p>
<p>I use shorthand to clean stuff up, but basically shorthand is</p>
<p>$returnvalue=(condition ?  "true" : "false");<br />
Example:</p>
<p>$is_loggedin=(isset($_SESSION["loggedin"]) ? 1 : 0);<br />
After this statement, if the session variable is set, $is_loggedin will equal 1, else it will equal 0.</p>
<p>At the top of your PHP page type the following:</p>
<pre>&lt;?php
/*paging*/
Line 1: $pg=(isset($_GET["pg"]) &amp;&amp; is_numeric($_GET["pg"]) &amp;&amp; $_GET["pg"]&gt;0 ? $_GET["pg"] : 1);
Line 2: $rowsperpage=10;
Line 3: $offset=($pg&gt;1 ? ($pg-1)*$rowsperpage : 0 );
Line 4: $limitstr=" LIMIT $offset, $rowsperpage";
?&gt;</pre>
<p>Explanation:<br />
First of all Limit is : LIMIT $startrowid, $number of entries to return<br />
So LIMIT 55, 5  means start at row 55 and return 5 rows (55, 56, 57, 58, 59)</p>
<p><strong>Line 1</strong>: If in the query string the pg is set  e.g page.php?pg=4, and it is numeric, and greater than 0 we will assign the value of $_GET["pg"] to our $pg variable. If it is not set, or it is not numeric, or zero or less, we set the $pg variable to 1.<br />
<strong>Line 2</strong>: We set our rows per page<br />
<strong>Line 3</strong>: If the page is greater than 1, we set the offset to ($pg-1)*rowsperpage. If the page is 1 or less, we set offset to 0.<br />
<strong>Line 4</strong>: We create the limit string for the sql statement. This will output something like LIMIT x, y.<br />
(rowsperpage set to 10)</p>
<p>In this case page.php?pg=1<br />
$pg = 1;<br />
$offset = 0;<br />
$rowsperpage=10;<br />
$limitstr="LIMIT 0, 10"; //this string will pull 10 rows starting from row 0 (the first row) (rows 0-9)</p>
<p>In this case page.php?pg=3<br />
$pg=3;<br />
$offset= (3-1) * 10;  //offset calculates to 20<br />
$limitstr="LIMIT 20, 10";  //this string will pull 10 rows starting from  and including row 20 (rows 20-29)</p>
<p><strong>Ok. Now that we got that out of the way.</strong></p>
<p>We will use a simple query, then we will attach our new limit string (looking at case 2 above)<br />
$sql="SELECT * FROM MEMBERS $limitstr"; //sql = "SELECT * FROM MEMBERS LIMIT 20,10";<br />
$res=mysql_query($sql);<br />
while($row=mysql_fetch_array($res)){</p>
<p>//processing if you need to<br />
}</p>
<p>We also need to know the total count for this table<br />
$sql="SELECT COUNT(memberid) FROM MEMBERS";<br />
$res=mysql_query($sql);<br />
$row=mysql_fetch_array($res);<br />
$total = $row[0];</p>
<p>Now we know the total count.</p>
<p>So we need to calculate the number of pages. That is done by this formula:<br />
($total/$rowsperpage);<br />
Now in the case of 26 members and 5 rows per page. Using this formula 26/5 =  5.2pages, so we need to round up!</p>
<p>$maxpage=ceil($total/$rowsperpage); //max page is now 6.</p>
<p>Ok, we got everything set up <img src='http://theinfiniteloopblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Just need to print out our paging!<br />
Wherever you want your paging setup add this:</p>
<pre>&lt;?php
print '&lt;div class="paging"&gt;&lt;ul&gt;';
for($i=1; $i&lt;=$maxpage; $i++){
  if($pg==$i)
     print '&lt;li&gt;&lt;span&gt;'.$i.'&lt;/span&gt;&lt;/li&gt;';
  else
     print '&lt;li&gt;&lt;a href="?pg='.$i.'"&gt;'.$i.'&lt;/a&gt;&lt;/li&gt;';
}
print "&lt;/ul&gt;&lt;/div&gt;";
?&gt;</pre>
<p>Then we can do some nice styling (Mess around with margins to setup spacing)<br />
CSS<br />
div.paging {text-align: center; margin-top: 5px;}<br />
div.paging span{font-weight: bold;}<br />
div.paging ul{overflow: hidden;}<br />
div.paging ul li{width: 10px; float: left; margin: 0px 10px 5px 0px;}</p>
<p>This will page across until it hits the edge of the div, then it will move to the next line.</p>
<p>ENJOY and comment ! <img src='http://theinfiniteloopblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://theinfiniteloopblog.com/dbs/easy-simple-paging-using-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

