|
The Generic PHP Page Navigation Class
Adding a page navigation system to a Web page database record listing
can be accomplished using a simple generic class. We are assuming here that the Web
server is running on PHP v5+ and using an MS-SQL database. PHP v5+
allows us to take advantage of true OOP techniques. Shortening a Web
page generated list of records into one page at a time sends the
formatted output from server to client much quicker than the entire
record set. The generic PHP class below makes that task easy, since
it employs OOP. The class name is PageNav for page navigation. It is
an abstract class because it allows one to define the methods for each
of the header, record list, and footer output. These methods may
include sub-queries, additional logic and complicated layouts for your
customization. The ability to customize the output and behavior of the
header for example, would enable support for sorting the output based
on the clicked field(s).
The compressed file archive includes the PageNav class and two examples,
which may be downloaded from the downloads section. The two examples
are based on the ways in which database records are retrieved by the
PageNav object on the PHP enabled Web server.
There are four parts to implementing the PageNav class that will be
explained in sequence below:
Part 1: Database Record Retrieval
PageNav relies on a database query to retrieve the record set for
display. It uses information pertaining to the record set to determine
the record count, page count and ranges at an offset relative to the
entire record set. There are two options for obtaining the database
record set and passing it to the PageNav object.
Database Connection & SQL Query
The PageNav object can take the MS-SQL database connection parameters
(DB server hostname or IP address, account information, and database
name). The connection is then established, and a query is sent in the
form of an SQL statement string. A call to methods
set_connection($host, $user, $password, $dbname) and
set_query ($sql, $rpp) complete this part.
View file: Listing_Query.php for an example.
Query Resource Parameter Passing
The PageNav object can take the MS-SQL database resource, which is the
result of the query made prior to initializing the PageNav object. The
connection and query yield the record set resource without intervention
of the PageNav object. A call to method set_result ($result, $rpp);
completes this part. Note that prior to this call the first parameter
must contain an already assigned MS-SQL database query resource (using
mssql_query and other mssql* functions prior to it).
View file: Listing_Result.php for an example.
Option 2 - Query Resource Parameter Passing shown here:
|
// obtain the query result resource and pass it to the navigation class
$link = mssql_connect ("127.0.0.1", "user", "password");
$sql = "SELECT id, field1, field2 FROM db_name.dbo.table_name ORDER BY field1";
$result = mssql_query ($sql, $link);
$listing = new MyList ("Listing_Result.php"); // parameters? (file.php?p=4)
$listing->set_result ($result, 5); // query resource, # records per page
$listing->set_cyclic (false); // no circular navigation
$listing->output ();
Code View - dynamicreport.com
|
Part 2: Initialize Navigation Data
It is necessary to initialize the data internal to the PageNav object
once it has been instantiated using one of two methods from part 1.
The attributes are based on the record set as follows:
|
$this->rpp; // records per page
$this->pg; // current page of record set
$this->pgs; // total record set pages
$this->ttl; // total records in entire record set
$this->alt; // alternating background (1 or 0)
$this->rec; // starting record of page relative to entire record set
$this->recs; // current record count relative to page
$this->index; // current record count relative to entire record set
$this->cyclic; // circular navigation beyond limits (true or false)
$this->phpfile; // web page file that implements this PageNav object
$this->arr_data; // array of all field values for the current record
Code View - dynamicreport.com
|
These attributes are available to your custom page navigation sub-class,
which defines the header, record list and footer output methods. The
call to methods set_query() or set_result() in part 1 commence
initialization of the PageNav object data implicitly, as it is necessary
for page navigation.
Part 3: Define and Display Record Listings
Before the page navigation object can be used, a sub-class must be
defined that implements the following abstract methods based on your
custom display needs:
- Header Output
The method out_header() displays the first output, which may include a
title for the listing, and the beginning portion of the HTML table that
encompasses the record listing to follow.
- Record Listing Output
The method out_record() displays a single record entity. This method is
called as many times as there are records on the page based on the
records per page value. The information may be displayed in table rows
and cells.
- Footer Output
The method out_footer() displays the last section of output, which may
include the closing HTML table tag that encompasses the record listing.
|
include ("PageNav.php");
class MyList extends PageNav
{ // class to customize the listing output - modify this to any extent
function __construct ($phpfile)
{ // this is required
parent::__construct ($phpfile);
}
function out_header ()
{ // displayed at the very beginning of the list
echo "<p align = \"center\">Web Page Screen Title</p>\n";
echo "<table width = \"100%\" border = \"0\">\n";
if ($this->ttl <= 0)
echo "<tr><td colspan = \"4\">No records found.</td></tr>\n";
else
echo "<tr><td>#</td><td>Record ID</td>".
"<td>Field 1</td><td>Field 2</td></tr>\n";
}
function out_record ()
{ // display the result set records
echo "<tr style = \"background: #".($this->alt ? "DEDEDE" : "EDEDED")."\">".
"<td>{$this->index}.</td><td>{$this->arr_data['id']}</td>".
"<td>{$this->arr_data['field1']}</td>".
"<td>{$this->arr_data['field2']}</td></tr>\n";
}
function out_footer ()
{ // displayed at the very bottom of the list
echo "</table>\n";
}
}
Code View - dynamicreport.com
|
Note that all class attributes in part 2 can be referenced in your
extended PageNav class, since they are inhereted by your custom defined
sub-class. Any of the methods you define can reference the attributes of
the parent PageNav class. The code snippet below defines an example of a
sub-class for implementing the customization of the page navigation class.
Once the sub-class has been instantiated and setup using either of the
options in part 1 it is time to call the method output(). The header,
record listing, footer and navigation components are displayed on the Web
page.
Part 4: Display Page Navigation
A method to display the page navigation component after your custom footer
output is included in the PageNav class. This method is generic, and can
be overridden by your own implementation for a more customized page
navigation look and feel. This may be accomplished by adding a definition
for the function out_navigation() {} method in your sub-class where the
custom output functions are defined.
|