Forum Moderators: coopster
function pager($datafile, $numrecs=5)
{
(file_exists($datafile)&&count(file($datafile))>0)?$this->totalrecs=array_reverse(file($datafile)):die('data file invalid.');
(is_int($numrecs)&&$numrecs>0)?$this->numrecs=$numrecs:die('invalid number of records'.$numrecs);
}// end function pager
function displayrecords($page)
{
$numpages=ceil(count($this->totalrecs)/$this->numrecs);
if (!preg_match("/^\d{1,2}$/",$page)¦¦$page<1¦¦$page>$numpages)
{
$page=1;
}
$records=array_slice($this->totalrecs, ($page-1)*$this->numrecs, $this->numrecs);
foreach ($records as $row)
{
$columns=explode(',',$row);
foreach ($columns as $column)
{
$this->output.=$column.' ';
}//end foreach loop
$this->output.='<br>';
}//end foreach loop
if ($page>1)
{
$this->output.='<a href="'.$_SERVER['PHP_SELF'].'?page='.($page-1).'"><<previous</a> ';
}// end if
for($i=1; $i<=$numpages; $i++)
{
($i!=$page)?$this->output.='<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i.'</a> ':$this->output.=$i.' ';
}// end for loop
if ($page<$numpages)
{
$this->output.=' <a href="'.$_SERVER['PHP_SELF'].'?page='.($page+1).'">next>></a>';
}// end if
return $this->ouput;
}//end function displayrecords
}// end class pager
if (!pager)
{
echo "pager function didn't work.";
}
if (!displayrecords)
{
echo "displayrecords function didn't work.";
}
?>
The code should work but nothing shows up on the page.
here is the code that goes on the page:
<html>
<head>
<title>
Paginating with php
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>Record listing</p>
<?php
require ('pager.php');
$pager=&new pager('data.dat');
$page=$_GET['page'];
echo $pager->displayrecords($page);
?>
</body>
</html>
Thanks,
Steven.
You might be able to force them by using:
error_reporting(E_ALL ^ E_NOTICE);
This really depends on your server set up.
Regarding the pagination, you haven't added the dollar sign before the variable "$pager" and "$displayrecords" in both if statements.
Incorrect:
if (!pager)
{
echo "pager function didn't work.";
}
if (!displayrecords)
{
echo "displayrecords function didn't work.";
}
Correct:
if (!$pager)
{
echo "pager function didn't work.";
}if (!$displayrecords)
{
echo "displayrecords function didn't work.";
}
I hope i have helped you a little, please let me know how you get on - also i am not sure if the logic behind what you are trying to achieve will actually work! maybe i am wrong :-)
Del