Home arrow MySQL arrow Page 3 - Building A Dynamic MySQL Paging Class With PHP

Building A Dynamic MySQL Paging Class With PHP

One handy class that Joe always keeps in his PHP toolbox is a custom MySQL recordset paging class, which he developed about 5 months ago. In this article Joe describes how he created the MySQL recordset paging class with PHP and also shows us how to use it with three cool examples!

Author Info:
By: Joe O'Donnell
Rating: 4 stars4 stars4 stars4 stars4 stars / 49
April 04, 2002
  1. · Building A Dynamic MySQL Paging Class With PHP
  2. · The class
  3. · The RecNav constructor
  4. · The ShowRecs function
  5. · Using the RecNav class
  6. · Conclusion

print this article

Building A Dynamic MySQL Paging Class With PHP - The RecNav constructor
(Page 3 of 6 )

Before we jump into how the RecNav constructor works, note that our RecNav class contains a number of variables, each prefixed with two underscores:

var $__linkId;

var $__dbType;

var $__query;

var $__template;

var $__templateHeader;

var $__templateFooter;

var $__recsPerPage;

The RecNav constructor really only validates the parameters passed to it, assigning default values to them if they're out of range or of the wrong type, etc. The constructor starts by making sure that the link identifier parameter is valid, by sending a dummy query to the MySQL server:

// Validate constructor parameters

if(!@mysql_query("SELECT 1", $LinkIdentifier))

{ die("MYSQL link identifier is invalid"); }



$this->__linkId = $LinkIdentifier;


If the link identifier isn't valid then the constructor calls the die() function with an error message. If the link identifier is valid, the __linkId variable is assigned its reference. Next, we validate the $Query parameter with a regular expression. It must contain "SELECT" as its first characters in order for it to be considered valid:

if(!ereg("^SELECT", $Query))

{ die("Invalid query: query must start with 'SELECT'"); }


{ $this->__query = $Query; }

The three template variables are then validated. If they are passed in, but passed as empty, then they are given default values that are defined within the same file as the class:

if($Template == "")

{ $this->__template = DEFAULT_TEMPLATE; }


{ $this->__template = $Template; }

if($TemplateHeader == "")

{ $this->__templateHeader = DEFAULT_TEMPLATE_HEADER; }


{ $this->__templateHeader = $TemplateHeader; }

if($TemplateFooter == "")

{ $this->__templateFooter = DEFAULT_TEMPLATE_FOOTER; }


{ $this->__templateFooter = $TemplateFooter; }

Lastly, we make sure that the $RecsPerPage parameter is numeric and valid. We then assign its value to the __recsPerPage variable:

if(!is_numeric($RecsPerPage) || $RecsPerPage < 1)

{ $this->__recsPerPage = DEFAULT_NUM_RECS; }


{ $this->__recsPerPage = $RecsPerPage; }

That's all of the code for the RecNav constructor. Because the constructor is so flexible, we can call it in a number of different ways:

// Return 10 records per page

$r = new RecNav($s, "SELECT * FROM books ORDER BY bookId ASC", "", "", "", 10);

// Return the default number of records and use default templates:

$r = new RecNav($s, "SELECT * FROM books");

// Return 20 records and use custom record template

$r = new RecNav($s, "SELECT * FROM books", "", "<tr><td><| row0 |></td></tr>", "", 20);

blog comments powered by Disqus

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials