Part of the EllisLab Network
   
 
Page Navigator Library
Posted: 01 November 2008 10:03 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  36
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 ? 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) ? '&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 .= '
<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&rsaquo; ' : '<a >_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
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120532 Total Logged-in Users: 34
Total Topics: 126594 Total Anonymous Users: 3
Total Replies: 665537 Total Guests: 393
Total Posts: 792131    
Members ( View Memberlist )
Newest Members:  Rach123Glenn StavaImfaelSMS!persanulandrewknightKittx77AmityBetbulJahhn