Page 1 of 1

help with Building A Dynamic MySQL Paging Class With PHP

Posted: Fri Jul 19, 2002 11:00 am
by gilbertwang
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 &nbsp;
$newRow = ereg_replace("<\| row[0-9] \|>", "&nbsp;", $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;

}

Posted: Fri Jul 19, 2002 8:01 pm
by gnu2php
The problem might be that you have $r->ShowRecs($page); and ShowRecs doesn't appear to be a member of the RecNav class. Does ShowRecs($page); work (without the $r->)? Maybe you need to put the ShowRecs function inside the RecNav class.

does anyone have a script that works

Posted: Fri Jul 19, 2002 9:12 pm
by gilbertwang
Does anyone have a script that works, I would like to implement a paging class something like this

page 1 2 3 4 5

Posted: Fri Jul 19, 2002 9:52 pm
by gnu2php
I'm not sure if this is want you had in mind, but if you go to http://liberty.dnsprotect.com/~devnetwo ... php?p=7369, you'll see two source code items I posted on another thread.

yes...that's exactly what I'm looking!

Posted: Fri Jul 19, 2002 11:09 pm
by gilbertwang
Thanks man, you are great. I have tested the array and is working but how do I put the data from mysql into that array.

let's say I am pulling up information from a table called member and there are 3 fields that I would like to display name, area,information, regtime
--------------------------------------------------------------------------
$sql = "select name, information, regtime from member order by regtime";
----------------------------------------------------------------------------
so how should i place the info into the array?
----------------------------------
$array = array("data");
----------------------------------
I would like to place them into rows and different color in each row. And I have created the function to display the row color.
---------------------------------------------------------------------------
function row_color($i){
$bg1 = "#0099FF"; // color one
$bg2 = "#336699"; // color two

if ( $i%2 ) {
return $bg1;
} else {
return $bg2;
}
}

----------------------------------------------------------------------------

where can I place this code in yours to display the row table.

//Starts the table
echo "<table bgcolor=#FFFFFF border=0 cellpadding=1 cellspacing=1>\n";

Create the contents of the table.
for( $i = 0; $i < $row = mysql_fetch_array($result); $i++){
//echo "<TR>\n"
//."<TD bgcolor=".row_color($i).">".$row["name"]."</TD>\n"
//."<TD bgcolor=".row_color($i).">".$row["area"]."</TD>\n"
//."<TD bgcolor=".row_color($i).">".$row["information"]."</TD>\n"
//."<TD bgcolor=".row_color($i).">".$row["regtime"]."</TD>\n"
//."</TR>";
}
echo "</TABLE>"

Posted: Sat Jul 20, 2002 12:03 am
by gnu2php
You can do it by putting an array in an array:

Code: Select all

$array = array();

for( $i = 0; $i < $row = mysql_fetch_array($result); $i++)&#123;

	array_push($array,
				array(
					'name' => $row&#1111;"name"],
					'area' => $row&#1111;"area"],
					'information' => $row&#1111;"information"],
					'regtime' => $row&#1111;"regtime"],
				));
&#125;
Then you can print the array like this:

Code: Select all

echo "<table bgcolor=#FFFFFF border=0 cellpadding=1 cellspacing=1>\n";

for ($i = $start; $i < ($num_items + $start); $i++)
&#123;
	echo "<TR>\n"
	."<TD bgcolor=".row_color($i).">".$array&#1111;$i]&#1111;"name"]."</TD>\n"
	."<TD bgcolor=".row_color($i).">".$array&#1111;$i]&#1111;"area"]."</TD>\n"
	."<TD bgcolor=".row_color($i).">".$array&#1111;$i]&#1111;"information"]."</TD>\n"
	."<TD bgcolor=".row_color($i).">".$array&#1111;$i]&#1111;"regtime"]."</TD>\n"
	."</TR>";
&#125;

echo "</TABLE>";