Part of the EllisLab Network
   
 
Page Navigator Library
Posted: 01 November 2008 09:03 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  08-03-2007

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 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 $this->_numofpage;
    if (
$end_page $this->_lastpage$end_page $this->_lastpage;
    
    
$links = ($this->_currentpage == 1) ? '&laquo;First ' '<a >_url.'/page/1').'" title="First Page">'.'&laquo;First'.'</a> ';
    
$links.= ($this->_currentpage == 1) ? '&lsaquo;Prev ' : '<a >_url.'/page/'.($this->_currentpage-1)).'" title="First Page">'.'&lsaquo;Prev'.'</a';
    
    if ($start_page > $this->_numofpage) $links .= '
<>_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&rsaquo' : '<>_url.'/page/'.($this->_currentpage+1)).'" title="Next Page">'.'Next&rsaquo;'.'</a> ';
    
$links.= ($this->_currentpage == $this->_lastpage) ? 'Last&raquo;' '<a >_url.'/page/'.$this->_lastpage).'" title="Last Page">'.'Last&raquo;'.'</a>';

    return 
$links;
  }
  
}
?> 

CI GREAT!!!! Thanks Derek smile

Profile