Page 1 of 1

class pager

Posted: Sat Feb 24, 2007 8:59 pm
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: Sun Feb 25, 2007 5:38 am
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.

Posted: Tue Feb 27, 2007 10:40 am
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.