class pager

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

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
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Thumb down, sorry :( Until the word "LIMIT" (as in SQL) appears in there, it's no good in my book.

Code: Select all

$i = 1; 
                while($i <= $total_page) 
                { 
                        $str .= " <a href='?page=$i'>$i</a> "; 
                        $i++; 
                }
Shorter means more readable, there's no point in replacing 'for' with 'while' in your case:

Code: Select all

for ($i = 1 ; $i <= $total_page ; ++$i)
      $str .= " <a href='?page=$i'>$i</a> ";

Also remove the private methods which are called only once, none of their callers is longer than a "hand" so there's no need to split them.
There are improperly named methods -- do you way in Englih "get total record" or "get total records" or even "get records count".

You don't seem to check if the user-specified page is outside the available records.
User avatar
veridicus
Forum Commoner
Posts: 86
Joined: Fri Feb 23, 2007 9:16 am

Post by veridicus »

Agreed, the query needs a LIMIT. For large data sets this is a must. Also, when you switch to LIMIT, don't use SQL_CALC_FOUND_ROWS to figure out how many pages of data you'll have. Use simple previous and next links. Your database will thank you for it.
Post Reply