Page 1 of 1

class pager

Posted: Wed Feb 14, 2007 10:33 am
by zyklone
This class pager is for large number of records from the database and you want to divide it for example 5 records per page. it is easy to navigate your page.

it is also easy to use.

Code: Select all

class Pager
{
	var $current_page;
	var $record_per_page;
	var $result;
	var $total_record;
	
	// counter
	var $row = 0;
	
	function pager($sql, $record_per_page, $current_page)
	{	
		if ($current_page == 0 || $current_page == '')
		{
			$this->current_page = 1;
		}
		else
		{
			$this->current_page = $current_page;
		}

		$this->record_per_page = $record_per_page;
		
		$this->result = mysql_query($sql);
		
		$this->total_record = $this->_getTotalRecord();
		
		$row_number = $this->_getRowNumber();
		$this->_seekRow($row_number);
	}
	
	function _seekRow($row_number)
	{
		if ($row_number > $this->_getTotalPage() && $row_number <= 0)
		{
			return false;
		}
		//echo $row_number;
		mysql_data_seek($this->result, $row_number - 1);
		
	}
		
	function _getTotalPage()
	{
		return ceil($this->total_record / (float) $this->record_per_page);
	}
	
	function _getTotalRecord()
	{
		$total_record = mysql_num_rows($this->result);
		
		return $total_record;
	}
	
	function _getRowNumber()
	{
		//echo "(". $this->current_page . " * " . $this->record_per_page . ") - " . $this->record_per_page . " + 1<BR>";
		return ($this->current_page * $this->record_per_page) - $this->record_per_page + 1;
	}
	
	function fetchObject()
	{
		$return = false;
		if ($this->row < $this->record_per_page)
		{
			$this->row++;
			$return = mysql_fetch_object($this->result);
		}
		
		return $return;
	}

	function getPageNav()
	{
		$total_page = $this->_getTotalPage();
		$i = 1;
		while($i <= $total_page)
		{
			$str .= " <a href='?page=$i'>$i</a> ";
			$i++;
		}
		
		return $str;
	}
}
Sample Usage:

Code: Select all

$sql = "SELECT * FROM tbl_data";

// 2nd parameter is the number of record per page

$pager = new Pager($sql, 4, $_GET['page']);

while($row = $pager->fetchObject())
{
	echo $row->firstname;
	echo "<BR>";
}

echo $pager->getPageNav();

Posted: Wed Feb 14, 2007 12:26 pm
by feyd
Are you asking a question?

Posted: Thu Feb 15, 2007 9:05 am
by zyklone
feyd wrote:Are you asking a question?
huh? im not asking any question i just want to share my code...hmmm....!!!

Posted: Thu Feb 15, 2007 10:16 am
by Mordred
Have you actually tried this? With more than 10 records in the table? With 10000?
Hint: you have to use LIMIT, otherwise it's going to pull every record in the table. And it's going to print too many page links.

I also dislike that you use this to pull the actual objects from the table, but that's maybe a personal preference. The other problem is quite severe though.

Posted: Thu Feb 15, 2007 2:24 pm
by Christopher
Thanks for your contribution zyklone. You might want to ask that this thread be moved to Code Critique so you can get input on your code. There are also a number of threads on pager classes, here are a couple:

MySQL specific:
viewtopic.php?t=58124

Database independent:
viewtopic.php?t=43777