<?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>PHP snippets, tutorials and scripts - Bits of PHP</title>
	<atom:link href="http://www.bitsofphp.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bitsofphp.com</link>
	<description>PHP snippets, tutorials and scripts</description>
	<lastBuildDate>Tue, 25 Oct 2011 22:29:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>&#194;&#163; UTF-8 Encoding Issue</title>
		<link>http://www.bitsofphp.com/utf-8-encoding-issue/</link>
		<comments>http://www.bitsofphp.com/utf-8-encoding-issue/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 22:29:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=51</guid>
		<description><![CDATA[When applying the function htmlentities on some POST input from a user submitted form you may come across an issue with character encoding where characters such as £ and © are not displaying correctly and are prepended with the &#194; character. This is typically caused by a UTF-8 encoding issue and the solution is quite...]]></description>
			<content:encoded><![CDATA[<p>When applying the function htmlentities on some POST input from a user submitted form you may come across an issue with character encoding where characters such as £ and © are not displaying correctly and are prepended with the &Acirc; character. This is typically caused by a UTF-8 encoding issue and the solution is quite easy. Simply pass in the character set to the htmlentities like this.</p>
<pre name="code" class="php">htmlentities($value, ENT_COMPAT | ENT_HTML401, 'UTF-8');</pre>
<p>The &#8216;ENT_COMPAT | ENT_HTML401&#8242; paramater is there simply because it is the default for this function, but the important bit is passing in &#8216;UTF-8&#8242; in the third parameter.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/utf-8-encoding-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shared hosting change PHP max execution time</title>
		<link>http://www.bitsofphp.com/shared-hosting-change-php-max-execution-time/</link>
		<comments>http://www.bitsofphp.com/shared-hosting-change-php-max-execution-time/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 23:10:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=48</guid>
		<description><![CDATA[When using a shared host the PHP execution time limit set by your host can sometimes be too low. Many hosts specifically set a low max execution time for any PHP scripts run on their servers to avoid any of their customers scripts slowing down and clogging up the servers resources. If for example one...]]></description>
			<content:encoded><![CDATA[<p>When using a shared host the PHP execution time limit set by your host can sometimes be too low. Many hosts specifically set a low max execution time for any PHP scripts run on their servers to avoid any of their customers scripts slowing down and clogging up the servers resources. If for example one customer on a shared host was to run a poorly designed PHP script it could potentially bring the whole server and every other customers using it to a complete halt.</p>
<p>The workaround which by the way is not supported by all hosts is to set the max PHP execution time using htaccess. Do to so create a file named &#8216;.htaccess&#8217; in the root of your websites directory with the following contents.</p>
<pre name="code" class="php">php_value max_execution_time 60</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/shared-hosting-change-php-max-execution-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get current page ID in WordPress</title>
		<link>http://www.bitsofphp.com/get-current-page-id-in-wordpress/</link>
		<comments>http://www.bitsofphp.com/get-current-page-id-in-wordpress/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 23:08:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=46</guid>
		<description><![CDATA[Getting the current page ID in a WordPress theme is just as easy as getting the post ID. In fact, they both use the exact same method and the ID&#8217;s for both WordPress posts and pages are stored in the same variable. From within the loop you can simply echo or access the ID property...]]></description>
			<content:encoded><![CDATA[<p>Getting the current page ID in a WordPress theme is just as easy as getting the post ID. In fact, they both use the exact same method and the ID&#8217;s for both WordPress posts and pages are stored in the same variable.</p>
<p>From within the loop you can simply echo or access the ID property of the global post object, like so: $post->ID. Just in case that&#8217;s not simple enough, here&#8217;s a PHP snippet of how to get the current page (or post) ID in WordPress.</p>
<pre name="code" class="php"><?php if(have_posts()): ?>
	<?php while(have_posts()):the_post(); ?>
		<?php echo $post->ID; ?><br/>
	<?php endwhile; ?>
<?php endif; ?>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/get-current-page-id-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is a number divisable</title>
		<link>http://www.bitsofphp.com/is-a-number-divisable/</link>
		<comments>http://www.bitsofphp.com/is-a-number-divisable/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 21:58:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=43</guid>
		<description><![CDATA[The following simple PHP function returns true or false depending on whether the given number is wholly divisible. This is particularly useful within loops to determine if the current loop counter is at a specific position (e.g. the last column in a HTML table output loop). function is_divisible($number, $divideby = 1) { if($number % $divideby...]]></description>
			<content:encoded><![CDATA[<p>The following simple PHP function returns true or false depending on whether the given number is wholly divisible. This is particularly useful within loops to determine if the current loop counter is at a specific position (e.g. the last column in a HTML table output loop).</p>
<pre name="code" class="php">function is_divisible($number, $divideby = 1)
{
	if($number % $divideby == 0)
		return true;
	else
		return false;
}</pre>
<p>Here&#8217;s an example of the function in use. Let&#8217;s say we want to loop through the numbers 1 to 9 inclusive and display it in a 3&#215;3 grid. We can use the above function to do exactly that.</p>
<pre name="code" class="php">for($i = 1; $i <= 0; $i++)
{
	echo $i;
	if(is_divisble($i, 3))
		echo "<br/>";
}</pre>
<p>The result of running the above snippet of code will be that after every 3rd number a line break is inserted making the rest continue on the next line down. It&#8217;s a very simple example but this PHP function can be used for all sorts of tasks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/is-a-number-divisable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get class and method names in CodeIgniter</title>
		<link>http://www.bitsofphp.com/get-class-and-method-names-in-codeigniter/</link>
		<comments>http://www.bitsofphp.com/get-class-and-method-names-in-codeigniter/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 21:58:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=39</guid>
		<description><![CDATA[When developing applications on top of the CodeIgniter PHP framework it can be quite useful sometimes to know exactly what class and method is currently being run in your code. Doing so is a very easy task. CodeIgniter provides you with quite a simple way of accessing the class and method names but we&#8217;re going...]]></description>
			<content:encoded><![CDATA[<p>When developing applications on top of the CodeIgniter PHP framework it can be quite useful sometimes to know exactly what class and method is currently being run in your code. Doing so is a very easy task.</p>
<p>CodeIgniter provides you with quite a simple way of accessing the class and method names but we&#8217;re going to make it even easier. You can normally access these via the $this->router->class and $this->router->method properties but here I&#8217;ve wrapped them into two separate functions.</p>
<h3>Get CodeIgniter class name</h3>
<pre name="code" class="php">function current_class()
{
	$CI = &#038;get_instance();
	return $CI->router->class;
}</pre>
<h3>Get CodeIgniter method name</h3>
<pre name="code" class="php">function current_method()
{
	$CI = &#038;get_instance();
	return $CI->router->method;
}</pre>
<p>You can place these functions pretty much anywhere although you should probably put them with the rest of your common functions. Wrapping them in the two functions shown above makes it much simpler to use and less chance for error. Displaying the CodeIgniter class and method names has always been pretty straight forward and now it&#8217;s even easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/get-class-and-method-names-in-codeigniter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add days, months, weeks or years to any date</title>
		<link>http://www.bitsofphp.com/add-days-months-weeks-or-years-to-any-date/</link>
		<comments>http://www.bitsofphp.com/add-days-months-weeks-or-years-to-any-date/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 16:05:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=36</guid>
		<description><![CDATA[Working with dates in PHP can be a little tricky if you&#8217;re not 100% sure on what your doing. Some systems deal with Unix timestamps and others such as MySQL use the longer format for dates. Either way it&#8217;s quite common practice to have to add to these dates. Whether you&#8217;re adding days, months, weeks,...]]></description>
			<content:encoded><![CDATA[<p>Working with dates in PHP can be a little tricky if you&#8217;re not 100% sure on what your doing. Some systems deal with Unix timestamps and others such as MySQL use the longer format for dates. Either way it&#8217;s quite common practice to have to add to these dates. Whether you&#8217;re adding days, months, weeks, years or even hours, seconds and minutes it&#8217;s easily done. And this is how.</p>
<h3>Adding date/time to Unix timestamps</h3>
<pre name="code" class="php">// Get the date and time right now
$dateandtime = time();

// Add and return the Unix timestamp
$unixTimestamp = strtotime(date("Y-m-d h:i:s", $dateandtime) . " +1 second");
$unixTimestamp = strtotime(date("Y-m-d h:i:s", $dateandtime) . " +1 minute");
$unixTimestamp = strtotime(date("Y-m-d h:i:s", $dateandtime) . " +1 hour");
$unixTimestamp = strtotime(date("Y-m-d h:i:s", $dateandtime) . " +1 day");
$unixTimestamp = strtotime(date("Y-m-d h:i:s", $dateandtime) . " +1 week");
$unixTimestamp = strtotime(date("Y-m-d h:i:s", $dateandtime) . " +1 month");
$unixTimestamp = strtotime(date("Y-m-d h:i:s", $dateandtime) . " +1 year");

// Output the Unix timestamp
echo $unixTimestamp;</pre>
<h2>Adding date/time to MySQL dates</h3>
<pre name="code" class="php">// Get the date and time right now
$dateandtime = date("Y-m-d  h:i:s");

// Working with Unix timestamps
$readableDate = date("Y-m-d h:i:s", strtotime($dateandtime  . " +1 second"));
$readableDate = date("Y-m-d h:i:s", strtotime($dateandtime  . " +1 minute"));
$readableDate = date("Y-m-d h:i:s", strtotime($dateandtime  . " +1 hour"));
$readableDate = date("Y-m-d h:i:s", strtotime($dateandtime  . " +1 day"));
$readableDate = date("Y-m-d h:i:s", strtotime($dateandtime  . " +1 week"));
$readableDate = date("Y-m-d h:i:s", strtotime($dateandtime  . " +1 month"));
$readableDate = date("Y-m-d h:i:s", strtotime($dateandtime  . " +1 year"));

// Output the date (already in readable format)
echo $readableDate;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/add-days-months-weeks-or-years-to-any-date/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upload an image using Java and PHP</title>
		<link>http://www.bitsofphp.com/upload-an-image-using-java-and-php/</link>
		<comments>http://www.bitsofphp.com/upload-an-image-using-java-and-php/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 22:04:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=19</guid>
		<description><![CDATA[Whilst working on a recent project I needed a way of transferring images (screenshots to be specific) from the client machine to a web server hosted in the cloud. My choice of platform was Java on the client machine and then PHP on the server side to recieve and process the upload. So basically I...]]></description>
			<content:encoded><![CDATA[<p>Whilst working on a recent project I needed a way of transferring images (screenshots to be specific) from the client machine to a web server hosted in the cloud. My choice of platform was Java on the client machine and then PHP on the server side to recieve and process the upload.</p>
<p>So basically I needed to upload an image to a web server using Java and PHP. Reading and writing simple data to and from a web server with Java is pretty easy really but when it comes to transferring files things get that little bit tougher. This is because when uploading files the content type of the HTTP request changes from plain text to multipart making it trickier. You could either write a whole load of extra code to deal with this or you could simply send the data over as plain text and then just let PHP deal with it. Here’s how it’s done.</p>
<pre name="code" class="java">// Setup vars
HttpURLConnection httpUrlConnection;
OutputStream outputStream;
BufferedInputStream fileInputStream;
BufferedReader serverReader;
int totalBytes;
int bytesTrasferred;
String response = "";
String serverResponse = "";
String localFileName = "mypicture.jpg";

// Establish a connection
httpUrlConnection=(HttpURLConnection)new URL("http://www.example.com/upload.php").openConnection();
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
outputStream = httpUrlConnection.getOutputStream();

// Buffered input stream
fileInputStream = new BufferedInputStream(new FileInputStream(localFileName));

// Get the size of the image
totalBytes = fileInputStream.available();

// Loop through the files data
for(int i=0; i < totalBytes; i++)
{
	// Write the data to the output stream
	outputStream.write(fileInputStream.read());
	bytesTrasferred = i + 1;
}

// Close the output stream
outputStream.close();

// New reader to get server response
serverReader = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));

// Read the servers response
serverResponse = "";
while((response = serverReader.readLine()) != null)
{
	serverResponse = serverResponse + response;
}

// Close the buffered reader
serverReader.close();

// Close the file input stream
fileInputStream.close()</pre>
<pre name="code" class="php">// Config
$uploadBase = "img/";
$uploadFilename = time() . ".jpg";
$uploadPath = $uploadBase . $uploadFilename;

// Upload directory
if(!is_dir($uploadBase))
	mkdir($uploadBase);

// Grab the data
$incomingData = file_get_contents('php://input');

// Valid data?
if(!$incomingData)
	die("No input data");

// Write to disk
$fh = fopen($uploadPath, 'w') or die("Error opening file");
fwrite($fh, $incomingData) or die("Error writing to file");
fclose($fh) or die("Error closing file");

echo "Success";</pre>
<p>And that’s all there is. By placing the PHP script above onto a suitable web server and then executing the Java code you’ll be able to quite easily and painlessly transfer images (or any other file types) from the desktop client to the web.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/upload-an-image-using-java-and-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get the current WordPress page number</title>
		<link>http://www.bitsofphp.com/get-the-current-wordpress-page-number/</link>
		<comments>http://www.bitsofphp.com/get-the-current-wordpress-page-number/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 21:37:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=17</guid>
		<description><![CDATA[WordPress as you probably already know is a very powerful content publishing platform and is used by hundreds of thousands of people all over the world. Despite all of the great things which you can do with WordPress out of the box there are a few things missing, one of which is getting the current...]]></description>
			<content:encoded><![CDATA[<p>WordPress as you probably already know is a very powerful content publishing platform and is used by hundreds of thousands of people all over the world.</p>
<p>Despite all of the great things which you can do with WordPress out of the box there are a few things missing, one of which is getting the current page number. For WordPress theme designers or those who just want to add a little customization to their WordPress themes this can be a very handy feature &#8211; it lets you find out the current page number.<span id="more-17"></span></p>
<p>Finding the current page number in WordPress consists of just a single line of code so it&#8217;s super easy. And here it is.</p>
<pre name="code" class="php">$pageNumber = (get_query_var('paged')) ? get_query_var('paged') : 1;</pre>
<p>This will get the current page number and store it into the $pageNumber variable. Then you can do whatever you like with it. Simple!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/get-the-current-wordpress-page-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Increase PHP allowed execution time</title>
		<link>http://www.bitsofphp.com/increase-php-allowed-execution-time/</link>
		<comments>http://www.bitsofphp.com/increase-php-allowed-execution-time/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 21:36:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=15</guid>
		<description><![CDATA[With a default install of PHP the scripts have a limited execution time in which they much execute, do their thing, and finish. Quite often particularly in shared hosting environment the default limit just isn&#8217;t enough. This is quite often the case if you&#8217;re trying to upload large files or process a lot of data...]]></description>
			<content:encoded><![CDATA[<p>With a default install of PHP the scripts have a limited execution time in which they much execute, do their thing, and finish. Quite often particularly in shared hosting environment the default limit just isn&#8217;t enough. This is quite often the case if you&#8217;re trying to upload large files or process a lot of data at once.</p>
<p>If you haven&#8217;t already seen it you will be greeted with the following error message if your PHP script reaches the execution time limit.</p>
<blockquote><p>Fatal error: Maximum execution time of 60 seconds exceeded in /location/of/php/script.php on line 101</p></blockquote>
<p>The solution to this problem is usually quite a simple one as PHP lets you increase the maximum script execution time quite easily. There are two ways of doing it, the first of which involved editing the php.ini configuration file. To change the PHP script execution time using the php.ini file simply open it up, do a search for &#8220;max_execution_time&#8221; and increase whatever the value is set to. The number used should be in seconds.<span id="more-15"></span></p>
<p>If however you don&#8217;t have access to the php.ini file (most shared hosting companies do not allow this) then you&#8217;re still in with a chance. It&#8217;s possible to change PHPs maximum execution time inside your scripts and it&#8217;s very easy to do.</p>
<pre name="code" class="php">set_time_limit(120);</pre>
<p>The above code snippet will set the PHP maximum execution time to be 120 seconds which should be plenty of time for most scripts. This line of code should be put at the very beginning of your PHP scripts making sure that nothing has been sent to the browser before your script has reached this line (otherwise it won&#8217;t work).</p>
<p>Alternatively if you don&#8217;t have a specific time limit in mind it is possible to tell your server to run your scripts endlessly with the following.</p>
<pre name="code" class="php">set_time_limit(0);</pre>
<p>Setting the seconds value to be zero means your script has no execution time limit and will therefore just keep going. Try to avoid this if possible though: sometimes it&#8217;s good for there to be limits in place as it stops your scripts from using up too many system resources.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/increase-php-allowed-execution-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP array pagination</title>
		<link>http://www.bitsofphp.com/php-array-pagination/</link>
		<comments>http://www.bitsofphp.com/php-array-pagination/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 21:35:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>

		<guid isPermaLink="false">http://www.bitsofphp.com/?p=13</guid>
		<description><![CDATA[Pagination in PHP is a topic covered by a lot of tutorials and is therefore quite saturated. Although I&#8217;m not going to introduce any wild new concepts into this tutorial I will explain how you can use pagination for data held within an array. But why would you want to do that? Why would anyone...]]></description>
			<content:encoded><![CDATA[<p>Pagination in PHP is a topic covered by a lot of tutorials and is therefore quite saturated. Although I&#8217;m not going to introduce any wild new concepts into this tutorial I will explain how you can use pagination for data held within an array.</p>
<p>But why would you want to do that? Why would anyone want to provide pagination for data within an array in PHP? Normally you&#8217;d only need to paginate your data if you&#8217;ve got quite a lot of it in which case you&#8217;re most likely using some sort of database. With database systems of course pagination can be achieved relatively easily by specifying the offset parameters in an SQL query. But what if your data didn&#8217;t come from a database table and instead came from a flat file.<span id="more-13"></span></p>
<p>Take a look at the following code which shows exactly how it&#8217;s done.</p>
<pre name="code" class="php">// Data, normally from a flat file or some other source
$data = "Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7|Item 8|Item 9|Item 10";

// Put our data into an array
$dataArray = explode('|', $data);

// Get the current page
$currentPage = trim($_REQUEST[page]);

// Pagination settings
$perPage = 3;
$numPages = ceil(count($dataArray) / $perPage);
if(!$currentPage || $currentPage > $numPages)
	$currentPage = 0;

$start = $currentPage * $perPage;
$end = ($currentPage * $perPage) + $perPage;

// Extract ones we need
foreach($dataArray AS $key => $val)
{
	if($key >= $start &#038;&#038; $key < $end)
		$pagedData[] = $dataArray[$key];
}</pre>
<p>Now for the explanation. To begin with I have created the $data array which contains a long line of items split up by the pipe '|' character. Realistically this would be real data however just for this tutorial I'll keep it simple. Then, using the explode() function I've cut up the $data variable into an array using '|' as the delimeter.</p>
<p>Line 8 simply gets the current page number if one is provided.</p>
<p>Lines 11 to 17 are all to do with the simple math calculations which make this array pagination work. Firstly set how many items we'd like to display per page into the variable, $perPage. In the example above I've set this to 3.</p>
<p>On line 12 we're working out how many pages there are going to be. This can be done by dividing the total number of items (by using the count() function) by the items per page value. Notice on this line that I'm also using the ceil() function. This basically rounds the number up (e.g. 5.134 becomes 6).</p>
<p>We then have a simply if statement on lines 13 and 14. It's basically saying that if no page number has been provided or if the provided page number is more than the number of pages, set it to 0. This stops people from trying to access pages which have no items.</p>
<p>On lines 19 and 20 we're setting the $start and $end variable which you might recognize if you've done pagination using SQL queries before. The $start variable holds the lowest ID of the item which can be displayed on this page. The $end variable is maximum cap which the items ID can be to be displayed (actually, it's one above, but this depends on how you do your if statement on line 22).</p>
<p>Great, we're nearly there. Now, on line 20 we start a foreach statement which loops through each of our data items. Inside this loop is a simple if statement to see if the id of the current data item is above (or equal to) the $start value and below the $end value. If it is then we place a copy of it into our $pagedData array.</p>
<p>Once the foreach loop has finished the $pagedData array now contains all of the data items which we should be displaying on the current page. All we have to do now is to loop through and display them. This has been shown in the following code snippet.</p>
<pre name="code" class="php">foreach($pagedData AS $item)
	echo $item . "<br/>";</pre>
<p>As far as displaying the data goes that's it. All we need to do now is to display the pagination links to let you navigate your way through the pages. Here's the code for that.</p>
<pre name="code" class="php">if($currentPage > 0 &#038;&#038; $currentPage < $numPages)
	echo '<a href="?page=' . ($currentPage - 1) . '">&laquo; Previous page</a><br/>';

if($numPages > $currentPage &#038;&#038; ($currentPage + 1) < $numPages)
	echo '<a href="?page=' . ($currentPage + 1) . '" class="right">Next page &raquo;</a><br/>';</pre>
<p>The above snippet consists of two quite simple if statements. The first is for displaying the "previous page" link and the second is for displaying the "next page" link.</p>
<p>Starting with the first, the if statement checking to see if the current page is more than 0 (you wouldn't have a previous link on the very first page) and if the current page is less than the total number of pages (to avoid displaying it on pages with no data).</p>
<p>The second if statement is checking to see if the total number of pages is more than the current page number (so you're not vieiwng the last page) and if there are any more pages after the current page.</p>
<p>Simple, right? That's all that you need to do to get simple PHP array pagination working. You should of course tweak the code provided here to fit your own purposes. Your data will most likely be more complex and you may want to display the items differently - now that you know how, it's all up to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bitsofphp.com/php-array-pagination/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

