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!
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"); }
else
{
$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'"); }
else
{ $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: