class pager

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
zyklone
Forum Commoner
Posts: 29
Joined: Tue Nov 28, 2006 10:25 pm

class pager

Post 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();
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you asking a question?
User avatar
zyklone
Forum Commoner
Posts: 29
Joined: Tue Nov 28, 2006 10:25 pm

Post by zyklone »

feyd wrote:Are you asking a question?
huh? im not asking any question i just want to share my code...hmmm....!!!
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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
(#10850)
Post Reply