Answer the question
In order to leave comments, you need to log in
Setting up pagination codeigniter, help the teapot?
There is a foreign CMS made on codeigniter. The page is loaded using javascript, when switching pages, a new link is not formed, in the browser line there is always a link to the main page. How to display a unique url for each page?
system/libraries/pagination.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Pagination {
* Constructor
* @access public
* @param array initialization parameters
function CI_Pagination($params = array())
{
if (count($params) > 0)
{
$this->initialize($params);
}
log_message('debug', "Pagination Class Initialized");
}
* Initialize Preferences
* @access public
* @param array initialization parameters
* @return void
function initialize($params = array())
{
if (count($params) > 0)
{
foreach ($params as $key => $val)
{
if (isset($this->$key))
{
$this->$key = $val;
}
}
}
}
* Generate the pagination links
* @access public
* @return string
function create_links()
{
// If our item count or per-page total is zero there is no need to continue.
if ($this->total_rows == 0 OR $this->per_page == 0)
{
return '';
}
// Calculate the total number of pages
$num_pages = ceil($this->total_rows / $this->per_page);
// Is there only one page? Hm... nothing more to do here then.
if ($num_pages == 1)
{
return '';
}
// Determine the current page number.
$CI =& get_instance();
if ($CI->uri->segment($this->uri_segment) != 0)
{
$this->cur_page = $CI->uri->segment($this->uri_segment);
// Prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
$this->num_links = (int)$this->num_links;
if ($this->num_links < 1)
{
show_error('Your number of links must be a positive number.');
}
if ( ! is_numeric($this->cur_page))
{
$this->cur_page = 0;
}
// Is the page number beyond the result range?
// If so we show the last page
if ($this->cur_page > $this->total_rows)
{
$this->cur_page = ($num_pages - 1) * $this->per_page;
}
$uri_page_number = $this->cur_page;
$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
// Calculate the start and end numbers. These determine
// which number to start and end the digit links with
$start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
$end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
// Add a trailing slash to the base URL if needed
$this->base_url = rtrim($this->base_url, '/') .'/';
// And here we go...
$output = '';
// Render the "First" link
if ($this->cur_page > $this->num_links)
{
$output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
}
// Render the "previous" link
if ($this->cur_page != 1)
{
$i = $uri_page_number - $this->per_page;
if ($i == 0) $i = '';
$output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
}
// Write the digit links
for ($loop = $start -1; $loop <= $end; $loop++)
{
$i = ($loop * $this->per_page) - $this->per_page;
if ($i >= 0)
{
if ($this->cur_page == $loop)
{
$output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
}
else
{
$n = ($i == 0) ? '' : $i;
$output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
}
}
}
// Render the "next" link
if ($this->cur_page < $num_pages)
{
$output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
}
// Render the "Last" link
if (($this->cur_page + $this->num_links) < $num_pages)
{
$i = (($num_pages * $this->per_page) - $this->per_page);
$output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
}
// Kill double slashes. Note: Sometimes we can end up with a double slash
// in the penultimate link so we'll kill all double slashes.
$output = preg_replace("#([^:])//+#", "\\1/", $output);
// Add the wrapper HTML if exists
$output = $this->full_tag_open.$output.$this->full_tag_close;
return $output;
}
}
<?php
class Pagination {
public $limit = 20; // how many per page
public $select_what = '*'; // what to select
public $add_query = '';
public $otherParams = '';
/* customize links */
public $next_r = '►';
public $previous_r = '◄';
public $thequery = FALSE;
public $is_ajax = FALSE;
public $link_id = '';
function __construct () {
$this->first_r = strtoupper ( Lang ( 'first' ) );
$this->last_r = strtoupper ( Lang ( 'last' ) );
}
function getQuery ( $return_q = FALSE ) {
$CI = & get_instance ();
if ( $this->thequery ) {
$query = $CI->db->query ( $this->thequery );
}
else {
$query = $CI->db->query ( 'SELECT
SQL_CALC_FOUND_ROWS
' . $this->select_what . '
FROM
' . $this->the_table . '
' . $this->add_query . '
LIMIT ' . $this->start . ', ' . $this->limit );
}
$this->nbItems = $CI->db->call_function ( 'result', $CI->db->call_function ( 'query', 'SELECT FOUND_ROWS() AS nbr' ), 0, 'nbr' );
if ( $return_q == FALSE ) {
return $this->nbItems;
}
else {
return $query;
}
}
function remove_double_slashes ( $string ) {
return preg_replace ( "/([^:])\/\/+/", "\\1/", $string );
}
function paginate () {
$nbItems = $this->nbItems;
if ( $nbItems <= $this->limit ) {
return;
}
else {
$allPages = ceil ( $nbItems / $this->limit );
$currentPage = floor ( $this->start / $this->limit ) + 1;
$pagination = "";
if ( $allPages > 9 ) {
$maxPages = ( $allPages > 7 ) ? 7 : $allPages;
if ( $allPages > 7 ) {
if ( $currentPage >= 1 && $currentPage <= $allPages ) {
$pagination .= ( $currentPage > 4 ) ? "... " : " ";
$minPages = ( $currentPage > 4 ) ? $currentPage : 5;
$maxPages = ( $currentPage < $allPages - 4 ) ? $currentPage : $allPages - 4;
for ( $i = $minPages - 4; $i < $maxPages + 4; $i ++ ) {
if ( $this->is_ajax ) {
if ( $i == $currentPage ) {
$pagination .= "<a href=\"javascript:void(0);\" class=\"current\">" . $i . "</a>";
}
else {
$pagination .= "<a href=\"javascript:void(0)\" onclick=\"ajax_paginate('" . $this->filePath . "/" . ( ( $i - 1 ) * $this->limit ) . $this->otherParams . "/" . print_unique_id () . "','" . $this->link_id . "')\">" . $i . "</a>";
}
}
else {
if ( $i == $currentPage ) {
$pagination .= "<a href=\"javascript:void(0);\" class=\"current\">" . $i . "</a>";
}
else {
$pagination .= "<a href=\"" . $this->remove_double_slashes ( $this->filePath . "/" . ( ( $i - 1 ) * $this->limit ) . $this->otherParams ) . "\">" . $i . "</a>";
}
}
}
$pagination .= ( $currentPage < $allPages - 4 ) ? "... " : "";
}
else {
$pagination .= "... ";
}
}
}
else {
for ( $i = 1; $i < $allPages + 1; $i ++ ) {
if ( $this->is_ajax ) {
if ( $i == $currentPage ) {
$pagination .= "<a href=\"javascript:void(0);\" class=\"current\">" . $i . "</a>";
}
else {
$pagination .= "<a href=\"javascript:void(0)\" onclick=\"ajax_paginate('" . $this->filePath . "/" . ( ( $i - 1 ) * $this->limit ) . $this->otherParams . "/" . print_unique_id () . "','" . $this->link_id . "')\">" . $i . "</a>";
}
}
else {
if ( $i == $currentPage ) {
$pagination .= "<a href=\"javascript:void(0);\" class=\"current\">" . $i . "</a>";
}
else {
$pagination .= "<a href=\"" . $this->remove_double_slashes ( $this->filePath . "/" . ( ( $i - 1 ) * $this->limit ) . $this->otherParams ) . "\">" . $i . "</a>";
}
}
}
}
if ( $currentPage > 1 ) {
if ( $this->is_ajax ) {
$pagination = "<a href=\"javascript:void(0)\" onclick=\"ajax_paginate('" . $this->filePath . "/0" . $this->otherParams . "/" . print_unique_id () . "','" . $this->link_id . "')\">" . $this->first_r . "</a><a href=\"javascript:void(0)\" onclick=\"ajax_paginate('" . $this->filePath . "/" . ( ( $currentPage - 2 ) * $this->limit ) . $this->otherParams . "/" . print_unique_id () . "','" . $this->link_id . "')\">" . $this->previous_r . "</a> " . $pagination;
}
else {
$pagination = "<a href=\"" . $this->remove_double_slashes ( $this->filePath . "/0" . $this->otherParams ) . "\">" . $this->first_r . "</a><a href=\"" . $this->remove_double_slashes ( $this->filePath . "/" . ( ( $currentPage - 2 ) * $this->limit ) . $this->otherParams ) . "\">" . $this->previous_r . "</a> " . $pagination;
}
}
if ( $currentPage < $allPages ) {
if ( $this->is_ajax ) {
$pagination .= "<a href=\"javascript:void(0)\" onclick=\"ajax_paginate('" . $this->filePath . "/" . ( $currentPage * $this->limit ) . $this->otherParams . "/" . print_unique_id () . "','" . $this->link_id . "')\">" . $this->next_r . "</a><a href=\"javascript:void(0)\" onclick=\"ajax_paginate('" . $this->filePath . "/" . ( ( $allPages - 1 ) * $this->limit ) . $this->otherParams . "/" . print_unique_id () . "','" . $this->link_id . "')\">" . $this->last_r . "</a>";
}
else {
$pagination .= "<a href=\"" . $this->remove_double_slashes ( $this->filePath . "/" . ( $currentPage * $this->limit ) . $this->otherParams ) . "\">" . $this->next_r . "</a><a href=\"" . $this->remove_double_slashes ( $this->filePath . "/" . ( ( $allPages - 1 ) * $this->limit ) . $this->otherParams ) . "\">" . $this->last_r . "</a>";
}
}
return "\n\t\t<div class=\"clear\"></div>\n\t\t<div class=\"pagination\">" . $this->remove_double_slashes ( $pagination ) . "\n\t\t</div>";
}
}
function __destruct () {
unset ( $this );
}
}
Answer the question
In order to leave comments, you need to log in
Pick a working example:
$this->load->library ('pagination');
$pg_cfg = array (
'base_url' => base_url () . 'gallery/show_album/' . $id . '/',
'total_rows' => count ($images),
'per_page' => $this->Gallery_model->get_images_number_per_page (),
'uri_segment' => 4,
'num_links' => 10,
'full_tag_open' => '<p class="paginator"> Страницы: ',
'full_tag_close' => '</p>',
'first_link' => 'первая',
'first_tag_open' => '<span>',
'first_tag_close' => '</span>',
'last_link' => 'последняя',
'last_tag_open' => '<span>',
'last_tag_close' => '</span>',
'prev_tag_open' => '<span>',
'prev_tag_close' => '</span>',
'next_tag_open' => '<span>',
'next_tag_close' => '</span>',
'num_tag_open' => '<span>',
'num_tag_close' => '</span>',
'cur_tag_open' => '<span class="current">',
'cur_tag_close' => '</span>',
);
$this->pagination->initialize ($pg_cfg);
$pg_links = $this->pagination->create_links ();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question