help with Building A Dynamic MySQL Paging Class With PHP
Posted: Fri Jul 19, 2002 11:00 am
I have read the tutorial at "http://www.devarticles.com/content.php? ... 110&page=1" written by Joe O;Donnell but I can't make it to work.
Can you guys help me, thanks so much.
This is the html page.
------------------------------------------------------------------------------------
<?
require("class.recnav.php");
global $HTTP_GET_VARS;
$page = $HTTP_GET_VARS["page"];
$s = mysql_connect("localhost", "username", "password");
$d = mysql_select_db("gossipl_cuppa", $s);
$r = new RecNav($s, "SELECT * FROM jokes ORDER BY joketime", "", "", "", 10);
echo $r->ShowRecs($page);
?>
-----------------------------------------------------------------------------
P.S: I get this error on this page on top whne I refresh it (Fatal error: Call to undefined function: showrecs() in /home/gossippl/public_html/morejokes.php on line 170)
-------------------------------------------------------------------------------
This is the class.recnav.php page. this page did not came up with any errors. The problem is when I refresh the html page, it tells me undefined function and I can't figure out what's wrong on the code below.
--------------------------------------------------------------------
<?
class RecNav
{
var $__linkId;
var $__dbType;
var $__query;
var $__template;
var $__templateHeader;
var $__templateFooter;
var $__recsPerPage;
function RecNav(&$LinkIdentifier, $Query, $Template=DEFAULT_TEMPLATE, $TemplateHeader=DEFAULT_TEMPLATE_HEADER, $TemplateFooter=DEFAULT_TEMPLATE_FOOTER, $RecsPerPage=DEFAULT_NUM_RECS) {
// 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; }
}
}
?>
<?
function ShowRecs($Page=1)
{
$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 .= "<a href='$PHP_SELF?page=" . ($Page-1) . "'><< Prev</a> |";
for($i = 1; $i < $numPages+1; $i++)
{
if($Page == $i)
{
// Bold the page and dont make it a link
$nav .= " <b>$i</b> |";
}
else
{
// Link the page
$nav .= " <a href='$PHP_SELF?page=$i'>$i</a> |";
}
}
// Can we have a link to the next page?
if($Page < $numPages)
$nav .= " <a href='$PHP_SELF?page=" . ($Page+1) . "'>Next >></a>";
// Strip the trailing pipe if there is one
$nav = ereg_replace("\|$", "", $nav);
$finalOutput .= "<div align='right'><font face='verdana' size='1'><br>Pages: $nav</font></div>";
return $finalOutput;
}
Can you guys help me, thanks so much.
This is the html page.
------------------------------------------------------------------------------------
<?
require("class.recnav.php");
global $HTTP_GET_VARS;
$page = $HTTP_GET_VARS["page"];
$s = mysql_connect("localhost", "username", "password");
$d = mysql_select_db("gossipl_cuppa", $s);
$r = new RecNav($s, "SELECT * FROM jokes ORDER BY joketime", "", "", "", 10);
echo $r->ShowRecs($page);
?>
-----------------------------------------------------------------------------
P.S: I get this error on this page on top whne I refresh it (Fatal error: Call to undefined function: showrecs() in /home/gossippl/public_html/morejokes.php on line 170)
-------------------------------------------------------------------------------
This is the class.recnav.php page. this page did not came up with any errors. The problem is when I refresh the html page, it tells me undefined function and I can't figure out what's wrong on the code below.
--------------------------------------------------------------------
<?
class RecNav
{
var $__linkId;
var $__dbType;
var $__query;
var $__template;
var $__templateHeader;
var $__templateFooter;
var $__recsPerPage;
function RecNav(&$LinkIdentifier, $Query, $Template=DEFAULT_TEMPLATE, $TemplateHeader=DEFAULT_TEMPLATE_HEADER, $TemplateFooter=DEFAULT_TEMPLATE_FOOTER, $RecsPerPage=DEFAULT_NUM_RECS) {
// 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; }
}
}
?>
<?
function ShowRecs($Page=1)
{
$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 .= "<a href='$PHP_SELF?page=" . ($Page-1) . "'><< Prev</a> |";
for($i = 1; $i < $numPages+1; $i++)
{
if($Page == $i)
{
// Bold the page and dont make it a link
$nav .= " <b>$i</b> |";
}
else
{
// Link the page
$nav .= " <a href='$PHP_SELF?page=$i'>$i</a> |";
}
}
// Can we have a link to the next page?
if($Page < $numPages)
$nav .= " <a href='$PHP_SELF?page=" . ($Page+1) . "'>Next >></a>";
// Strip the trailing pipe if there is one
$nav = ereg_replace("\|$", "", $nav);
$finalOutput .= "<div align='right'><font face='verdana' size='1'><br>Pages: $nav</font></div>";
return $finalOutput;
}