
September 24th, 2002, 10:36 AM
|
|
Contributing User
|
|
Join Date: Aug 2002
Posts: 232
Time spent in forums: < 1 sec
Reputation Power: 7
|
|
|
here it is
of course, you have to go through and tweak your colors and stuff for the default output. you can either pass your templates to it, or definie default templates, or use both, or whatever. you can call the class by doing:
$recs = new RecNav;
$recs->showRecs($dblink,$query,$template1,$template2,$tem plate3,$numrecs);
PHP Code:
<?php
// Default number of records
define("DEFAULT_NUM_RECS", "");
define("DEFAULT_TEMPLATE_HEADER", "");
define("DEFAULT_TEMPLATE", "");
define("DEFAULT_TEMPLATE_FOOTER", "");
class RecNav
{
function RecNav(&$LinkIdentifier, $Query, $Template=DEFAULT_TEMPLATE, $TemplateHeader=DEFAULT_TEMPLATE_HEADER, $TemplateFooter=DEFAULT_TEMPLATE_FOOTER, $RecsPerPage=DEFAULT_NUM_RECS, $db_object, $query_string)
{
// Validate constructor parameters
if(!@mysql_query("SELECT 1", $LinkIdentifier))
{ die("MYSQL link identifier is invalid"); }
else
{
$this->__linkId = $LinkIdentifier;
}
if(!ereg("^SELECT", $Query))
{ die("Invalid query: query must start with 'SELECT'"); }
else
{ $this->__query = $Query; }
if($Template == "")
{ $this->__template = DEFAULT_TEMPLATE; }
else
{ $this->__template = $Template; }
if($TemplateHeader == "")
{ $this->__templateHeader = DEFAULT_TEMPLATE_HEADER; }
else
{ $this->__templateHeader = $TemplateHeader; }
if($TemplateFooter == "")
{ $this->__templateFooter = DEFAULT_TEMPLATE_FOOTER; }
else
{ $this->__templateFooter = $TemplateFooter; }
if(!is_numeric($RecsPerPage) || $RecsPerPage < 1)
{ $this->__recsPerPage = DEFAULT_NUM_RECS; }
else
{ $this->__recsPerPage = $RecsPerPage; }
if(!$query_string == "N\A")
{ $this->__querystring = $query_string;
$this->__dbObject = $db_object; }
else
{ $this->__querystring = 0;
$this->__dbObject = 0; }
}
function ShowRecs($Page=1)
{
// Using the classes variables and the $Page variable,
// the ShowRecs function will query the database and
// return the records based on the $__template variable.
$finalOutput = $this->__templateHeader;
if($Page <= 1)
{
$Page = 1;
$query = $this->__query . " LIMIT 0, " . $this->__recsPerPage;
}
else
{ $query = $this->__query . " LIMIT " . (($Page-1) * $this->__recsPerPage) . ", " . $this->__recsPerPage; }
$result = mysql_query($query);
$hasRecords = mysql_num_rows($result) == 0 ? false : true;
if($hasRecords)
{
// At least one records returned from the query
while($row = mysql_fetch_row($result))
{
$newRow = $this->__template;
for($i = 0; $i < mysql_num_fields($result); $i++)
{
$newRow = str_replace("<| row" . $i . " |>", $row[$i], $newRow);
}
$finalOutput .= $newRow;
}
}
else
{
// No records returned from the query
$newRow = $this->__template;
$newRow = str_replace("<| row0 |>", "No records found", $newRow);
// Replace all template tags with
$newRow = ereg_replace("<\| row[0-9] \|>", " ", $newRow);
$finalOutput .= $newRow;
}
$finalOutput .= $this->__templateFooter;
// Build the recordset paging links
$numTotalRecs = mysql_num_rows(mysql_query($this->__query));
$numPages = ceil($numTotalRecs / $this->__recsPerPage);
$nav = "";
// Can we have a link to the previous page?
if($Page > 1)
$nav .= "<td class='pagenum'><a href='$PHP_SELF?page=" . ($Page-1) . "'><<</td> ";
for($i = 1; $i < $numPages+1; $i++)
{
if($Page == $i)
{
// Bold the page and dont make it a link
$nav .= " <td class='pagenum'><h6>$i</h6></td> ";
}
else
{
// Link the page
$nav .= " <td class='pagenum'><a href='$PHP_SELF?page=$i'>$i</a></td> ";
}
}
// Can we have a link to the next page?
if($Page < $numPages)
$nav .= " <td class='pagenum'><a href='$PHP_SELF?page=" . ($Page+1) . "'>>></a></td>";
// Strip the trailing pipe if there is one
$nav = ereg_replace("\|$", "", $nav);
$finalOutput .= "<br><div class='center'><Table border=o bordercolor=#888888 cellspacing=2 cellpadding=2 width=50 height=24><TR><TD class='page'><h6>Pages</td></tr></table><br><Table border=o bordercolor=#888888 cellspacing=2 cellpadding=2 width=50 height=24><TR> $nav</tr></table></div>";
return $finalOutput;
}
var $__linkId;
var $__dbType;
var $__query;
var $__template;
var $__templateHeader;
var $__templateFooter;
var $__recsPerPage;
var $__dbObject;
var $__querystring;
var $__queryarray;
}
?>
|