Forum Moderators: coopster

Message Too Old, No Replies

Problems paginating (can't figure out the problem)

         

steven420

8:06 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



Ok, I have been through the code over and over again and I can't seem to find the bug. I would greatly appreciate it if anyone can spot possible problems. Here is the code:
<?php

class pager
{
var $output='';
var $totalrecs;
var $numrecs;

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.'&nbsp;';
}//end foreach loop
$this->output.='<br>';

}//end foreach loop

if ($page>1)
{
$this->output.='<a href="'.$_SERVER['PHP_SELF'].'?page='.($page-1).'">&lt;&lt;previous</a>&nbsp;';
}// end if

for($i=1; $i<=$numpages; $i++)
{
($i!=$page)?$this->output.='<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i.'</a>&nbsp;':$this->output.=$i.'&nbsp;';
}// end for loop

if ($page<$numpages)
{
$this->output.='&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.($page+1).'">next&gt;&gt;</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.

scriptmasterdel

11:50 pm on Jan 18, 2007 (gmt 0)

10+ Year Member



It sounds like your error reporting has been switched off, this is why you see no errors.

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

steven420

8:33 pm on Jan 19, 2007 (gmt 0)

10+ Year Member



I can't believe I forgot those $. The class I have is from a tutorial I found. I'm not sure why it isn't working. I'm just trying to learn how to paginate. Thanks for the help.