PHP Recordset Paging

In this tutorial, we first look at creating a page that displayed the results of a database query, and showing all results returned to the user.

Imagine that you have a recordset containing 100 records, for example. Usually you wouldn't want to display all 100 records at once, as it's far too much information for a web site visitor to take in at once, and it can also make your web pages slow to load. Instead, it's much more desirable to be able to show the user 10 records at a time for example, and let them move back and forth between pages. A perfect example of this is a search engine such as Google, although it can be used on any web site that uses dynamic data.

Advertisement DMXzone Paginator PHP

Add the coolest page navigation to your site and make browsing through large lists of items or tables easy and quick. Choose from 22 different styles, used by many sites such as Digg, Yahoo, Flickr and much more, to fit perfectly with your design and display the total number of items.

All paging styles are fully CSS based, so you can always fine tune the colors and borders to your site design.

 


Overview

In this tutorial, we first look at creating a page that displayed the results of a database query, and showing all results returned to the user. We then look at the MySQL LIMIT command, which returns only certain records from the results obtained by a query, meaning you only get the records you are actually going to use, creating faster and more efficient queries. We use the LIMIT command to adapt our existing code so that it showed the results in pages of 5 records to a page.

We then create a dynamic navigation bar, which allows the users to quickly move backwards and forwards between pages. Finally we alter the code so that if results of a search are being shown, the search parameters are preserved as the user moves from page to page.

 

Table of Content:

  • What is Recordset Paging?
  • Step 1 - Creating some Example Data
  • Step 2 - Creating a Database Connection File
  • Step 3 - Creating Code to Create and Display a Recordset
    • 3.1 Creating the PHP code to read records from the Database
    • 3.2 Creating the HTML and PHP to display the Results
    • 3.3 Testing the Page
  • Step 4 - Adding Recordset Paging
    • 4.1 - The MySQL LIMIT command
    • 4.2 Adapting the existing code
    • 4.3 Creating a Dynamic Navigation Bar
    • 4.4 Testing the Complete Page
    • 4.5 - Using the Recordset Pages with the Results of a Search
  • Summary

4.3 Creating a Dynamic Navigation Bar

Creating a navigation bar is actually quite easy, and is achieved by adding the following block of PHP code beneath the main PHP code block at the top of the page.

<?php
// Create Dynamic Navigation Bar
$pageURL = $_SERVER['SCRIPT_NAME'];
$html = "";
if($currentPage > 1){
     // Create Previous Page Link (<<)
     $url = $pageURL . "?page=" . ($currentPage - 1);
     $html .= "<a href='" .  $url . "'> << </a>";
     $html .= "&nbsp;";
}
for($i=1; $i <= $totalPageNumber; $i++){
     // Create Numerical Page Links ( 1 2 3 etc )
     $url = $pageURL . "?page=" . $i;
     $html .= "<a href='" .  $url . "'>" . $i . "</a>";
     $html .= "&nbsp;";
}
if($currentPage < $totalPageNumber){
     // Create Next Page Link (>>)
     $url = $pageURL . "?page=" . ($currentPage + 1);
     $html .= "<a href='" .  $url . "'> >> </a>";
     $html .= "&nbsp;";
}
?>

Code Block 7 - Code to create a Dynamic Navigation Bar

As usual, we'll go through this code block by block.

// Create Dynamic Navigation Bar
$pageURL = $_SERVER['SCRIPT_NAME'];
$html = "";

First, we use the special server variable $_SERVER['SCRIPT_NAME'] to get the name of the current page, which we place in the variable $pageURL, so that we can use this in the navigation links. We then setup an empty variable called $html which were going to use to store the HTML for the navigation bar.

if($currentPage > 1){
     // Create Previous Page Link (<<)
     $url = $pageURL . "?page=" . ($currentPage - 1);
     $html .= "<a href='" .  $url . "'> << </a>";
     $html .= "&nbsp;";
}

We then check to see if the current page number is greater than 1, meaning we're not on the first page of records. If this is the case, we create a link to the previous page ($currentPage - 1) and construct a URL in the form script_name.php?page=page_number. We add a &nbsp; (non blanking space) to separate it from the page number that will be next to it.

for($i=1; $i <= $totalPageNumber; $i++){
     // Create Numerical Page Links ( 1 2 3 etc )
     $url = $pageURL . "?page=" . $i;
     $html .= "<a href='" .  $url . "'>" . $i . "</a>";
     $html .= "&nbsp;";
}

Next, we use a for loop, which will run once for every page number, and creates a link where the URL parameter page is set to the relevant page number e.g.

script_name.php?page=1

script_name.php?page=2

script_name.php?page=3

etc&

if($currentPage < $totalPageNumber){
     // Create Next Page Link (>>)
     $url = $pageURL . "?page=" . ($currentPage + 1);
     $html .= "<a href='" .  $url . "'> >> </a>";
     $html .= "&nbsp;";
}

Finally, we check that we're not on the last page of records, and if we're not we create a link where the page parameter is set to the current page number + 1, to take the user to the next page.

At this stage, we now have the complete HTML code for the navigation bar in the variable $html, and all we need to do is to echo this to the browser in our chosen position by adding the following PHP code.

<?php echo $html; ?>

Save the page before you move on.

Gareth Downes-Powell

Gareth Downes-PowellGareth has a range of skills, covering many computer and internet related subjects. He is proficient in many different languages including ASP and PHP, and is responsible for the setup and maintenance of both Windows and Linux servers on a daily basis.


In his daily web development work he uses the complete range of Macromedia software, including Dreamweaver MX, Flash MX, Fireworks MX and Director to build a number of websites and applications. Gareth has a close relationship with Macromedia, and as a member of Team Macromedia Dreamweaver, he has worked closely in the development of Dreamweaver, and was a beta tester for Dreamweaver MX.


On a daily basis he provides support for users in the Macromedia forums, answering questions and providing help on a range of different web related subjects. He has also written a number of free and commercial extensions for Dreamweaver MX, to further extend its capabilities using its native JavaScript API’s or C++.


As a web host, Gareth has worked with a range of different servers and operating systems, with the Linux OS as his personal favourite. Most of his development work is done using a combination of Linux, Apache and MySQL and he has written extensively about setting up this type of system, and also running Apache and MySQL under Windows.

See All Postings From Gareth Downes-Powell >>

Reviews

nice tutorial

May 24, 2009 by ijaz khattak

dear sir

i am facing a prblem in my project, if u can help me in that i will mail it t you.

thanks 

You must me logged in to write a review.