Home arrow PHP arrow Page 4 - Previous or Next? Paginating Records with PHP, part 1

Putting the "paginateRecords()" function into action: procedural record paging - PHP

Putting all of your content for a particular file on one page can be very user unfriendly to website visitors. Nobody likes to be confronted by a tiny vertical scroll bar! In this article, the first in a series, you will learn a simple way to paginate records from a text file using PHP.

TABLE OF CONTENTS:
  1. Previous or Next? Paginating Records with PHP, part 1
  2. The first step: paginating records in a procedural way
  3. A deeper look at the "paginatetRecords()" function
  4. Putting the "paginateRecords()" function into action: procedural record paging
By: Alejandro Gervasio
Rating: starstarstarstarstar / 23
April 26, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Let's jump straight in and test out our recently developed "paginateRecords()" function. With three lines of code, we're ready to paginate records on our Web pages. Here's a possible regular implementation:

<html>

<head>

<title>PAGINATING DATA WITH PHP</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>

<body>

<p>Record Listing</p>

<?php

require_once('pager.php');

$page=$_GET['page'];

echo paginateRecords('data.dat',$page);

?>

</body>

</html>

Let's be honest. The required code to make record paging is extremely short and understandable. We've just included the file that contains the function and invoked it, passing in the parameters the data file to be read and the page pointer to display the records. Just give it a try and add your own improvements. Simple and straightforward, isn't it?

However, there are some issues to be considered about this approach.  This solution might be suitabel for small websites, where you can live quite happily delivering content from simple text files. With larger applications, however, and with heavy loads and more demanding requirements, this approach falls short. What's more, we're mixing HTML markup with PHP code, which is not recommended at all.

So, can we do it any better? You bet. We must improve this technique by developing a solution definitely more portable for larger websites, and able to deal with a relational database system such as MySQL. That will be our next step in the paging record process.

Summarizing and further improvements

In this first part, we looked at a simple PHP function for paginating records extracted from a flat text file. Also, we saw how simply it can be implemented, with just a few lines of code. Of course, this procedural method supports many possible improvements, being a handy introduction to paging techniques. If you're already working with Web applications that require recordset paging very frequently with minor headaches, just take the function and play with it to meet your needs.

In the coming second part, we'll look at a mature OOP approach, defining a PHP class to efficiently handle recordset paging processes, considerably more suitable for larger applications. It sounds very promising. We have the tools and the knowledge for developing such a class. So, get ready for the next part, and stay tuned!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: