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

The first step: paginating records in a procedural way - 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.

  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



Our first method for paginating records might be easily developed by taking a procedural approach: using a flat text file as the data source for displaying information, and defining a PHP function that takes care of paginating the records. Sounds like something good to deploy, at least for the initial step, right?

Let's begin defining a simple text file, which contains several lines (our records), including information about a song's author and the song's title. The example file, which is called "data.dat," could be structured as listed below:

Chariots of fire|Vangelis

Friends of Mr. Cairo|Vangelis

Lucifer|The Alan Parson Project

The eye in the sky|Alan Parson Project

Caribean Blue|Enya

Only Time|Enya

Music|John Miles

The turn of a friendly card|The Alan Parson Project

Children|Robert Miles

One and One|Robert Miles


Mea Culpa|Enigma

Cherry Blossom Girl|Air

Hot stuff|Donna Summer

Bad Girls|Donna Summer

I will survive|Gloria Gaynor

Rimes and reasons|John Denver

Touch and go|Emerson,Lake and Powell

In Jeopardy|Roger Hogdson


As you can see, our text file containing information about several songs has been created, defining two fields for each line, author and title respectively, separated by a pipe ("|") character. This way we've established the overall structure for this simple flat database. Please don't start complaining about my musical preferences, because they're beyond the scope of this article. Let's focus our attention on the code listed above.

Once we have defined the data source file, we need to create a PHP function for retrieving the records from the file, and generating the corresponding paginating links. In fact, the process is pretty straightforward, as we'll see in a moment. Now, let's start writing the function to paginate records. I've named it "paginateRecords()", and its definition is the following:

function paginateRecords($dataFile,$page,$numRecs=5){


// validate data file

($dataFile)):die('Data file not valid.');

// validate number of records per page

('Invalid number of records '.$numRecs);

// calculate total of records


// validate page pointer




// retrieve records from flat file


// append records to output

foreach($data as $row){


foreach($columns as $column){



$output.='<br />';


// create previous link


$output.='<a href="'.$_SERVER['PHP_SELF'].'?page='.


// create intermediate links


($i!=$page)?$output.='<a href="'.$_SERVER


// create next link


$output.='&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?
page='.($page+1).'">Next&gt;&gt;</a> ';


// return final output

return $output;


Don't feel intimidated by the function's source code, since it's really easy to understand. In order to grasp the underlying logic, let's break down the code to have a detailed look at each section. Are you ready? Let's go for it.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: