This library automatically generate page navigator when query some data from database.
But this library does not work for query having “LIMIT” like this
SELECT * FROM some_table LIMIT 10
I hope some can help for this problem
And this is the code
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Pagenavigator
{
var $_init = FALSE;
var $CI = null;
var $_rowperpage = 10;
var $_currentpage = 1;
var $_lastpage = null;
var $_numofpage = 10;
var $_url = null;
var $_islast = FALSE;
var $_nodata = TRUE;
function Pagenavigator()
{
}
function init()
{
if ($this->_init) return TRUE;
$this->CI =& get_instance();
$this->_url = eregi_replace('/page/[^/]*', '', $this->CI->uri->uri_string());
$this->_currentpage = eregi('/page/([^/]*)', $this->CI->uri->uri_string(), $regs) ? (is_numeric($regs[1]) ? intval($regs[1]) : 1) : 1;
if ($this->_currentpage < 1) $this->_currentpage = 1;
$this->_init = TRUE;
}
function query($sql, $binds=FALSE)
{
$this->init();
$limit = $this->_rowperpage;
$offset = ($this->_currentpage-1) * $this->_rowperpage;
$query = $this->CI->db->query("SELECT * FROM ({$sql}) AS a LIMIT {$limit} OFFSET {$offset}", $binds);
// count last page
$qry = $this->CI->db->query("SELECT COUNT(*) AS row_count FROM ({$sql}) AS a", $binds);
$row = $qry->row_array();
$this->_nodata = $row['row_count']==0;
$this->_lastpage = $this->_nodata ? 1 : ceil($row['row_count']/$this->_rowperpage);
if ($this->_currentpage > $this->_lastpage) return FALSE;
return $query;
}
function get_links()
{
$this->init();
if ($this->_nodata) return 'No data found';
$start_page = (floor(($this->_currentpage-1)/$this->_numofpage) * $this->_numofpage) + 1;
$end_page = $start_page - 1 + $this->_numofpage;
if ($end_page > $this->_lastpage) $end_page = $this->_lastpage;
$links = ($this->_currentpage == 1) ? '«First ' : '<a >_url.'/page/1').'" title="First Page">'.'«First'.'</a> ';
$links.= ($this->_currentpage == 1) ? '‹Prev ' : '<a >_url.'/page/'.($this->_currentpage-1)).'" title="First Page">'.'‹Prev'.'</a> ';
if ($start_page > $this->_numofpage) $links .= '<a >_url.'/page/'.($this->_currentpage - $this->_numofpage)).'" title="More '.$this->_numofpage.' Previous Page">...</a> ';
for ($i=$start_page; $i<=$end_page; $i++)
{
$links .= $this->_currentpage==$i ? '<strong>'.$i.'</strong> ' :'<a >_url.'/page/'.$i).'">'.$i.'</a> ';
}
if ($this->_lastpage != $end_page)
{
$more_next = $this->_currentpage + $this->_numofpage;
if ($more_next > $this->_lastpage) $more_next = $this->_lastpage;
$links .= '<a >_url.'/page/'.$more_next).'" title="More '.$this->_numofpage.' Next Page">...</a> ';
}
$links.= ($this->_currentpage == $this->_lastpage) ? 'Next› ' : '<a >_url.'/page/'.($this->_currentpage+1)).'" title="Next Page">'.'Next›'.'</a> ';
$links.= ($this->_currentpage == $this->_lastpage) ? 'Last»' : '<a >_url.'/page/'.$this->_lastpage).'" title="Last Page">'.'Last»'.'</a>';
return $links;
}
}
?>
CI GREAT!!!! Thanks Derek
