Файл: incs/classes/pagination_class.php
Строк: 94
<? 
class Paging { 
private $page_size = 5; 
private $link_padding = 3; 
private $page_link_separator = ' '; 
private $next_page_text = 'следующая →'; 
private $prev_page_text = '← предыдущая'; 
private $result_text_pattern = 'Показано с %s по %s из %s'; 
private $page_var = 'p';
private $db; 
private $q; 
private $total_rows; 
private $total_pages; 
private $cur_page; 
public function __construct($db, $q='', $page_var='p') 
{ 
    $this->db = $db; 
    if ($q) $this->set_query($q); 
    $this->page_var = $page_var; 
    $this->cur_page = isset($_GET[$this->page_var]) && (int)$_GET[$this->page_var] > 0 ? (int)$_GET[$this->page_var] : 1; 
} 
public function set_query($q) 
{ 
    $this->q = $q; 
} 
public function set_page_size($page_size) 
{ 
    $this->page_size = abs((int)$page_size); 
} 
public function set_link_padding($padding) 
{ 
    $this->link_padding = abs((int)$padding); 
} 
public function get_page($q='', $order='ORDER BY `id` ASC') 
{ 
    if ($q) $this->set_query($q); 
    $this->order = $order;
    $r = $this->db->query( $this->query_paging($this->q) ); 
    $this->total_rows = array_pop($this->db->query('SELECT FOUND_ROWS()')->fetch_row()); 
    if ($this->page_size !== 0) $this->total_pages = ceil($this->total_rows/$this->page_size); 
     
    if ($this->cur_page > $this->total_pages) 
    { 
        $this->cur_page = $this->total_pages; 
        if ( $this->total_pages > 0 ) $r = $this->db->query( $this->query_paging($this->q) ); 
    } 
     
    return $r; 
} 
public function get_result_text() 
{ 
    $start = (($this->cur_page-1) * $this->page_size)+1; 
    $end = (($start-1+$this->page_size) >= $this->total_rows)? $this->total_rows:($start-1+$this->page_size); 
    return sprintf($this->result_text_pattern, $start, $end, $this->total_rows); 
} 
public function get_page_links($gurl) 
{ 
    if ( !isset($this->total_pages) ) return ''; 
    $page_link_list = array(); 
    $start = $this->cur_page - $this->link_padding; 
    if ( $start < 1 ) $start = 1; 
    $end = $this->cur_page + $this->link_padding-1; 
    if ( $end > $this->total_pages ) $end = $this->total_pages; 
    if ( $start > 1 )  $page_link_list[] = $this->get_page_link( $start-1, $start - 2 > 0 ? '...' : '', $gurl ); 
    for ($i=$start; $i <= $end; $i++)  $page_link_list[] = $this->get_page_link( $i, NULL, $gurl ); 
    if ( $end + 1 < $this->total_pages ) $page_link_list[] = $this->get_page_link( $end +1, $end + 2 == $this->total_pages ? '' : '...', $gurl ); 
    if ( $end + 1 <= $this->total_pages ) $page_link_list[] = $this->get_page_link( $this->total_pages, NULL, $gurl ); 
    $rtn = (!empty($page_link_list[0])) ? '<div class="postr">'.implode($this->page_link_separator, $page_link_list).'</div>' : '';
    return $rtn;
} 
public function get_next_page_link() 
{ 
    return isset($this->total_pages) && $this->cur_page < $this->total_pages ? $this->get_page_link( $this->cur_page + 1, $this->next_page_text ) : ''; 
} 
public function get_prev_page_link() 
{ 
    return isset($this->total_pages) && $this->cur_page > 1 ? $this->get_page_link( $this->cur_page - 1, $this->prev_page_text ) : ''; 
} 
private function get_page_link($page, $text='', $gurl) 
{
    if($this->total_rows > $this->page_size) {
    if (!$text)    $text = $page;
    if ($page != $this->cur_page) 
    { 
        $url = $gurl.'/page'.$page; 
        return '<a href="'.$url.'">'.$text.'</a>'; 
    }
    else return '<b>'.$page.'</b>';
    return $text; 
    }
}
private function query_paging() 
{ 
    $q = $this->q; 
    if ($this->page_size != 0) 
    { 
        //calculate the starting row 
        $start = ($this->cur_page-1) * $this->page_size; 
        //insert SQL_CALC_FOUND_ROWS and add the LIMIT 
        $q = preg_replace('/^SELECTs+/i', 'SELECT SQL_CALC_FOUND_ROWS ', $this->q)." ".$this->order." LIMIT {$start},{$this->page_size}"; 
    } 
    return $q; 
} 
} 
?>