Pagination current link not highlighting - php

I got a strange problem, my current pagination link is not highlighted the pagination urls I made looks like:
site.com/list/50/?some=value
Everything is working great, but the current pagination link in view is not highlighted. I checked CSS and it's ok, the problem comes from the library I guess.
This is my code. I can't see anything wrong in here:
$pagination['base_url'] = site_url('asd');
if($_SERVER['QUERY_STRING'] !==''){
$pagination['suffix'] = '/?'.$_SERVER['QUERY_STRING'];
}
$pagination['first_url'] = site_url('asd');
$pagination['total_rows'] = $total_rows[0]->total;
$pagination['first_link'] = ' First ';
$pagination['last_link'] = ' Last ';
$pagination['next_link'] = ' Next ';
$pagination['prev_link'] = ' Prev ';
$pagination['num_links'] = 5;
$this->pagination->initialize($pagination);
Any clue appreciated.
i post my library which i edited for markup also:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* #package CodeIgniter
* #author ExpressionEngine Dev Team
* #copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* #license http://codeigniter.com/user_guide/license.html
* #link http://codeigniter.com
* #since Version 1.0
* #filesource
*/
// ------------------------------------------------------------------------
/**
* Pagination Class
*
* #package CodeIgniter
* #subpackage Libraries
* #category Pagination
* #author ExpressionEngine Dev Team
* #link http://codeigniter.com/user_guide/libraries/pagination.html
*/
class CI_Pagination {
var $base_url = ''; // The page we are linking to
var $prefix = ''; // A custom prefix added to the path.
var $suffix = ''; // A custom suffix added to the path.
var $total_rows = ''; // Total number of items (database results)
var $per_page = 10; // Max number of items you want shown per page
var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
var $cur_page = 0; // The current page being viewed
var $first_link = '‹ First';
var $next_link = '>';
var $prev_link = '<';
var $last_link = 'Last ›';
var $uri_segment = 3;
var $full_tag_open = '<p class="CI-pagination">';
var $full_tag_close = '</p>';
var $first_tag_open = '<span class="pagination-first">';
var $first_tag_close = ' </span>';
var $last_tag_open = '<span class="pagination-last"> ';
var $last_tag_close = '</span>';
var $first_url = ''; // Alternative URL for the First Page.
var $cur_tag_open = '<span class="pagination-current"> <a href="javascript:void(0)" class="btn btn-small">';
var $cur_tag_close = '</a></span>';
var $next_tag_open = '<span class="pagination-next"> ';
var $next_tag_close = ' </span>';
var $prev_tag_open = '<span class="pagination-prev"> ';
var $prev_tag_close = '</span>';
var $num_tag_open = '<span class="pagination-num"> ';
var $num_tag_close = '</span>';
var $page_query_string = FALSE;
var $query_string_segment = 'per_page';
var $display_pages = TRUE;
var $anchor_class = 'btn btn-small';
/**
* Constructor
*
* #access public
* #param array initialization parameters
*/
public function __construct($params = array())
{
if (count($params) > 0)
{
$this->initialize($params);
}
if ($this->anchor_class != '')
{
$this->anchor_class = 'class="'.$this->anchor_class.'" ';
}
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->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
{
if ($CI->input->get($this->query_string_segment) != 0)
{
$this->cur_page = $CI->input->get($this->query_string_segment);
// Prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
}
else
{
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;
// Is pagination being used over GET or POST? If get, add a per_page query
// string. If post, add a trailing slash to the base URL if needed
if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
{
$this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
}
else
{
$this->base_url = rtrim($this->base_url, '/') .'/';
}
// And here we go...
$output = '';
// Render the "First" link
if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
{
$first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
$output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
}
// Render the "previous" link
if ($this->prev_link !== FALSE AND $this->cur_page != 1)
{
$i = $uri_page_number - $this->per_page;
if ($i == 0 && $this->first_url != '')
{
$output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
}
else
{
$i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
$output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
}
}
// Render the pages
if ($this->display_pages !== FALSE)
{
// 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;
if ($n == '' && $this->first_url != '')
{
$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
}
else
{
$n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
}
}
}
}
}
// Render the "next" link
if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
{
$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
}
// Render the "Last" link
if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
{
$i = (($num_pages * $this->per_page) - $this->per_page);
$output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$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;
}
}
// END Pagination Class
/* End of file Pagination.php */
/* Location: ./system/libraries/Pagination.php */
According to my pagination library, all my pagination links are printed out as class="pagination-num" and the class="pagination-current" is added only to the first pagination link , unbelievable :/
When changing page, the pagination looks always as shown in this image, no changes between a page to another :/

Read this and find the "Customizing the "Current Page" Link" section. Use that to add custom markup then add appropriate CSS.
Edit: the OP found the following solution:
$pagination['uri_segment'] = 2;

Add these line and write css for .current class
$pagination['cur_tag_open'] = '<a href="javascript:void(0)" class="current">';
$pagination['cur_tag_close'] = '</a>';
$pagination['cur_page'] = {put current page no here};

Check your $config['uri_segment']. Otherwise handle from jquery. Do somethings like:-
Suppose your link:-
<div id="my-ci-link"><?php echo $links; ?></div>
write jQuery:-
$('#my-ci-link strong').css('color','green');

<pre>
if ($this->uri->segment(2))
$data['page'] = $this->uri->segment(2);
else
$data['page'] = 0;
$config["cur_page"] = $data['page'];
...
</pre>

Related

Codeigniter 3.x Ajax Pagination

Hello (it's my first post) I post because i really need help for paginate the results by category on codeigniter.
When i click on the link generated, the page loads to infinity. This pagination is ok on the index page for all results. But i need paginate by category.
thank you very very much in advance for you're help.
The Controler:
class Blog extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model(array('Blog_model'));
$this->load->library('Ajax_pagination');
$this->perPage = 3;
}
function category($category_id)//category page
{
$data = array();
//total rows count
$totalRec = count($this->Blog_model->getRowsByCat($category_id));
//pagination configuration
$config['target'] = '#postwrap';
$config['base_url'] = base_url().'blog/category/'.$category_id.'/ajaxPaginationCat';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
//get the posts data
$data['posts'] = $this->Blog_model->getRowsByCat($category_id,array('limit'=>$this->perPage));
//
$data['main_content'] = "blog/category";
$this->load->view('template',$data);
}
function ajaxPaginationCat($category_id){
$page = $this->input->post('page');
if(!$page){
$offset = 0;
}else{
$offset = $page;
}
//total rows count
$totalRec = count($this->Blog_model->getRowsByCat($category_id));
//pagination configuration
$config['target'] = '#postwrap';
$config['base_url'] = base_url().'blog/category/'.$category_id.'/ajaxPaginationCat';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
//get posts data
$data['posts'] = $this->Blog_model->getRowsByCat($category_id,array('start'=>$offset,'limit'=>$this->perPage));
//load the view
$this->load->view('blog/ajax-pagination-data', $data, false);
}
The Model:
/*
* get rows from the posts table
*/
function getRowsByCat($category_id,$params = array()){
$this->db->select('*');
$this->db->from('blog_posts');
$this->db->order_by('post_id','desc');
if(!empty($category_id)){
$this->db->where('category_id',$category_id);
}
//set start and limit
if(array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit'],$params['start']);
}elseif(!array_key_exists("start",$params) && array_key_exists("limit",$params)){
$this->db->limit($params['limit']);
}
//get records
$query = $this->db->get();
//return fetched data
return ($query->num_rows() > 0)?$query->result_array():FALSE;
}
Library:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Pagination Class
*
* #package CodeIgniter
* #link http://codeigniter.com/user_guide/libraries/pagination.html
*
* Modified by CodexWorld.com
* #Ajax pagination functionality has added with this library.
* #It will helps to integrate Ajax pagination with loading image in CodeIgniter application.
* #TutorialLink http://www.codexworld.com/ajax-pagination-in-codeigniter-framework/
*/
class Ajax_pagination{
var $base_url = ''; // The page we are linking to
var $total_rows = ''; // Total number of items (database results)
var $per_page = 10; // Max number of items you want shown per page
var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
var $cur_page = 0; // The current page being viewed
var $first_link = 'First';
var $next_link = '»';
var $prev_link = '«';
var $last_link = 'Last';
var $uri_segment = 3;
var $full_tag_open = '<div class="pagination">';
var $full_tag_close = '</div>';
var $first_tag_open = '';
var $first_tag_close = ' ';
var $last_tag_open = ' ';
var $last_tag_close = '';
var $cur_tag_open = ' <b>';
var $cur_tag_close = '</b>';
var $next_tag_open = ' ';
var $next_tag_close = ' ';
var $prev_tag_open = ' ';
var $prev_tag_close = '';
var $num_tag_open = ' ';
var $num_tag_close = '';
var $target = '';
var $anchor_class = '';
var $show_count = true;
var $link_func = 'getData';
var $loading = '.loading';
/**
* 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;
}
}
}
// Apply class tag using anchor_class variable, if set.
if ($this->anchor_class != ''){
$this->anchor_class = 'class="' . $this->anchor_class . '" ';
}
}
/**
* 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){
$info = 'Showing : ' . $this->total_rows;
return $info;
}
// 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 = '';
// SHOWING LINKS
if ($this->show_count){
$curr_offset = $CI->uri->segment($this->uri_segment);
$info = 'Showing ' . ( $curr_offset + 1 ) . ' to ' ;
if( ( $curr_offset + $this->per_page ) < ( $this->total_rows -1 ) )
$info .= $curr_offset + $this->per_page;
else
$info .= $this->total_rows;
$info .= ' of ' . $this->total_rows . ' | ';
$output .= $info;
}
// Render the "First" link
if ($this->cur_page > $this->num_links){
$output .= $this->first_tag_open
. $this->getAJAXlink( '' , $this->first_link)
. $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
. $this->getAJAXlink( $i, $this->prev_link )
. $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
. $this->getAJAXlink( $n, $loop )
. $this->num_tag_close;
}
}
}
// Render the "next" link
if ($this->cur_page < $num_pages){
$output .= $this->next_tag_open
. $this->getAJAXlink( $this->cur_page * $this->per_page , $this->next_link )
. $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 . $this->getAJAXlink( $i, $this->last_link ) . $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;
?>
<script>
function getData(page){
$.ajax({
method: "POST",
url: "<?php echo $this->base_url; ?>"+page,
data: { page: page },
beforeSend: function(){
$('<?php echo $this->loading; ?>').show();
},
success: function(data){
$('<?php echo $this->loading; ?>').hide();
$('<?php echo $this->target; ?>').html(data);
}
});
}
</script>
<?php
return $output;
}
function getAJAXlink($count, $text) {
$pageCount = $count?$count:0;
return ''. $text .'';
}
}
// END Pagination Class
And the Ajax generated:
<script>
function getData(page){
$.ajax({
method: "POST",
url: "blog/category/1/ajaxPaginationCat/"+page,
data: { page: page },
beforeSend: function(){
$('.loading').show();
},
success: function(data){
$('.loading').hide();
$('#postwrap').html(data);
}
});
}
</script>
thank you so much!

How to change ajax pagination to load more or infinite scroll functionality

I located the AJAX pagaination fucntion of a plugin i'm using for wordpress called Events Manager. As of now when clicking on the pagination it loads the upcoming content on a next page .. however i want the loaded content to display under the initial content. So that users can keep scrolling down. Just like infinite scroll with a load more button. Anyone willing to help me out here or point me in the right direction?
Thanks in advance!
if(!function_exists('em_paginate')){ //overridable e.g. in you mu-plugins folder.
/**
* Takes a few params and determines a pagination link structure
* #param string $link
* #param int $total
* #param int $limit
* #param int $page
* #param array $data If supplied and EM_USE_DATA_ATTS is true/defined, this set of data will be stripped from the URL and added as a data-em-ajax attribute containing data AJAX can use
* #return string
*/
function em_paginate($link, $total, $limit, $page=1, $data=array()){
if($limit > 0){
$pagesToShow = defined('EM_PAGES_TO_SHOW') ? EM_PAGES_TO_SHOW : 10;
$url_parts = explode('?', $link);
$base_link = $url_parts[0];
$base_querystring = '';
$data_atts = '';
//Get querystring for first page without page
if( count($url_parts) > 0 ){
$query_arr = array();
parse_str($url_parts[1], $query_arr);
//if $data was passed, strip any of these vars from both the $query_arr and $link for inclusion in the data-em-ajax attribute
if( !empty($data) && is_array($data) && (!defined('EM_USE_DATA_ATTS') || EM_USE_DATA_ATTS) ){
//remove the data attributes from $query_arr
foreach( array_keys($data) as $key){
if( array_key_exists($key, $query_arr) ){
unset($query_arr[$key]);
}
}
//rebuild the master link, without these data attributes
if( count($query_arr) > 0 ){
$link = $base_link .'?'. build_query($query_arr);
}else{
$link = $base_link;
}
$data_atts = 'data-em-ajax="'.esc_attr(build_query($data)).'"'; //for inclusion later on
}
//proceed to build the base querystring without pagination arguments
unset($query_arr['page']); unset($query_arr['pno']);
$base_querystring = esc_attr(build_query($query_arr));
if( !empty($base_querystring) ) $base_querystring = '?'.$base_querystring;
}
//calculate
$maxPages = ceil($total/$limit); //Total number of pages
$startPage = ($page <= $pagesToShow) ? 1 : $pagesToShow * (floor($page/$pagesToShow)) ; //Which page to start the pagination links from (in case we're on say page 12 and $pagesToShow is 10 pages)
$placeholder = urlencode('%PAGE%');
$link = str_replace('%PAGE%', $placeholder, esc_url($link)); //To avoid url encoded/non encoded placeholders
//Add the back and first buttons
$string = ($page>1 && $startPage != 1) ? '<a class="prev page-numbers" href="'.str_replace($placeholder,1,$link).'" title="1"><<</a> ' : '';
if($page == 2){
$string .= ' <a class="prev page-numbers" href="'.esc_url($base_link.$base_querystring).'" title="2">back </a> ';
}elseif($page > 2){
$string .= ' <a class="prev page-numbers" href="'.str_replace($placeholder,$page-1,$link).'" title="'.($page-1).'"> </a> ';
}
//Loop each page and create a link or just a bold number if its the current page
for ($i = $startPage ; $i < $startPage+$pagesToShow && $i <= $maxPages ; $i++){
if($i == $page || (empty($page) && $startPage == $i)) {
$string .= ' <strong><span class="page-numbers current">'.$i.'</span></strong>';
}elseif($i=='1'){
$string .= ' <a class="page-numbers" href="'.esc_url($base_link.$base_querystring).'" title="'.$i.'"></a> ';
}else{
$string .= ' <a class="page-numbers" href="'.str_replace($placeholder,$i,$link).'" title="'.$i.'"></a> ';
}
}
//Add the forward and last buttons
$string .= ($page < $maxPages) ? ' <a class="next page-numbers" href="'.str_replace($placeholder,$page+1,$link).'" title="'.($page+1).'">load more</a> ' :' ' ;
$string .= ($i-1 < $maxPages) ? ' <a class="next page-numbers" href="'.str_replace($placeholder,$maxPages,$link).'" title="'.$maxPages.'">>></a> ' : ' ';
//Return the string
return apply_filters('em_paginate', '<span class="em-pagination" '.$data_atts.'>'.$string.'</span>');
}
}
}
If the plugin is using custom post types, you might be able to use this plugin as well ajax-load-more. You can also create an ajax request yourself.

codeigniter 3 customize pagination bootstrap

How can i customize my codeigniter links with bootstrap 4?
Im trying but i dont have luck with the a links.
thanks.
public function index($start=0)
{
if (!$this->session->userdata('user_id'))
{
redirect(base_url().'admin/login');
}
$this->load->model('M_Articulos');
$data['posts'] = $this->M_Articulos->select_posts(1, $start);
//paginacion
$this->load->library('pagination');
$config['base_url'] = base_url()."admin/index";
$config['total_rows'] = $this->M_Articulos->get_post_count();
$config['per_page'] = 1;
//paginacion estilos con bootstrap
$this->pagination->initialize($config);
$data['pages'] = $this->pagination->create_links(); //Links of pages
$this->load->view('admin/layouts/header');
$this->load->view('admin/modules/main', $data);
$this->load->view('admin/layouts/footer');
}
If you are using codeigniter-3,
check pagination library, if you are using default pagination library,
You need to make changes in create_links() function inside that as per your requirements.
Customizable code is here :
// Render the pages
if ($this->display_pages !== FALSE)
{
// Write the digit links
for ($loop = $start - 1; $loop <= $end; $loop++)
{
$i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop);
if ($i >= $base_page)
{
if ($this->cur_page === $loop)
{
// Current page
$output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
}
elseif ($i === $base_page)
{
// First page
$output .= $this->num_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
.$loop.'</a>'.$this->num_tag_close;
}
else
{
$append = $this->prefix.$i.$this->suffix;
$output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.'>'
.$loop.'</a>'.$this->num_tag_close;
}
}
}
}
// Render the "next" link
if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
{
$i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1);
$output .= $this->next_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
.$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
}
// Render the "Last" link
if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links + ! $this->num_links) < $num_pages)
{
$i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages);
$output .= $this->last_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>'
.$this->last_link.'</a>'.$this->last_tag_close;
}

how to configure pagination codeigniter?

Ive trying to make a pagination with CodeIgniter, it should be so simple according the manual of Codeigniter, even in the example is like this
« First < 1 2 3 4 5 > Last »
$config['total_rows'] = $this->searchdesc_model->queryallrows();
$config['per_page'] = '10';
$config['uri_segment'] =4;
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$config['cur_tag_open'] = '<b>';
$config['cur_tag_close'] = '</b>';
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['last_tag_open'] = '<p>';
$config['last_tag_close'] = '</p>'
$this->load->library('Company_Creation');
in the view I only call it like this pagination->create_links(); ?> (or I send it through the view when I call it from the controller, still I only get this
1 2 3 >
and there is no way to make it look like the exmaple, may sound so dummy but , anyone could help me with this? or have a problem similar?
Thanks
EDIT 1
$config['total_rows'] = $this->searchdesc_model->queryallrows();
$config['per_page'] = '5';
$config['uri_segment'] =4;
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$config['cur_tag_open'] = '<b>';
$config['cur_tag_close'] = '</b>';
$config['first_link'] = ' First';
$config['last_link'] = ' Last';
$config['last_tag_open'] = '<p>';
$config['last_tag_close'] = '</p>';
$config['next_link'] = '';
$config['next_tag_open'] = '<p id="nextbutton" style="padding-left:5px;">';
$config['next_tag_close'] = '</p>';
$config['prev_link'] = '';
$config['prev_tag_open'] = '<p id="prevbutton" style="padding-right:5px;">';
$config['prev_tag_close'] = '</p>';
$config['num_links']=4;
$data['retorno'] = $this->searchdesc_model->queryalldb($config['per_page'],$this->uri->segment(4,0));
$config['total_rows']=1000;
$this->pagination->initialize($config);
I did this according some advices Ive recieve, like you said when are many data it works good, still I'd LOVE to show first and next button all time, I set total_rows after my query (which I call with the right number of rows) , and I tried before too and the results are the same, I also need to show only 4 numbers and I am ustying numb_links ... still doesnt work (I dont know why Ci docs say should work..) any idea?
Thanks!
To produce what the example shows is actually pretty easy. You just need to extend the Pagination's Library to accommodate this. I was able to do this. No matter how many pages you show it still shows first, last, back arrow, and forward arrow.
If you are wanting to show 5 pages at all times with the foward and back stuff you need to have that many results to fill that page. Then you set num_links to what you would want before and after if on the 3rd page. So it would be 2. My changes if you are on the 1st page make it display 4 pages after when applicable. See image below. White is current page. Green is pages available.
Hopefully I've explained everything correctly and this works for you. Let me know.
Controller
$this->pagingConfig = array();
$this->pagingConfig['base_url'] = 'URL';
$this->pagingConfig['total_rows'] = 0;//TOTAL ROWS
$this->pagingConfig['cur_page'] = 0;//CURRENT PAGE NUMBER
$this->pagingConfig['per_page'] = 0;//YOUR RESULTS PER PAGE
$this->pagingConfig['num_links'] = 2;//NUMBER OF LINKS BEFORE AND AFTER CURRENT PAGE IF ON PAGE ONE WILL SHOW 4 PAGES AFTERWARDS IF YOU HAVE ENOUGH RESULTS TO FILL THAT MANY
$this->pagingConfig['first_link'] = "<< First";
$this->pagingConfig['last_link'] = "Last >>";
$this->pagingConfig['full_tag_open'] = "<div class='pagination'>";
$this->pagingConfig['full_tag_close'] = "</div>";
$this->pagingConfig['last_tag_open'] = "";
$this->pagingConfig['first_tag_close'] = "";
$this->pagingConfig['anchor_class'] = "page";
$this->pagination->initialize($this->pagingConfig);
$strPaging = $this->pagination->create_links();
EXTENDED PAGINATION LIBRARY CALL
function create_links()
{
// EDIT: ADDED THIS BECAUSE COULDN'T SEEM TO SET THIS ANYWHERE ELSE
if ($this->anchor_class != '')
{
$this->anchor_class = 'class="'.$this->anchor_class.'" ';
}
// 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->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
{
if ($CI->input->get($this->query_string_segment) != 0)
{
$this->cur_page = $CI->input->get($this->query_string_segment);
// Prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
}
else
{
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 = 1;
}
// 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);
}
// EDIT: DON'T NEED THIS THE WAY I'VE CHANGED IT
// $uri_page_number = $this->cur_page;
// $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
// EDIT: START OF MODIFIED START AND END TO WORK HOW I WANT
$totalLinks = ($this->num_links*2)+1;
if($totalLinks > ($this->total_rows/$this->per_page))
{
$totalLinks = ceil($this->total_rows/$this->per_page);
}
//first page
if($this->cur_page == 1)
{
$start = 1;
$end = $start + $totalLinks - 1;
}
//middle pages
elseif($this->cur_page + $this->num_links <= $num_pages && $this->cur_page - $this->num_links > 0)
{
$start = $this->cur_page - $this->num_links;
$end = $this->cur_page + $this->num_links;
}
//last couple of pages
elseif(($this->cur_page + $totalLinks) > $num_pages)
{
$start = $num_pages - $totalLinks + 1;
$end = $num_pages;
//check to see if this is in the first half of links so it doesn't jump the paging
if($this->cur_page <= $this->num_links)
{
$start = 1;
$end = $start + $totalLinks - 1;
}
}
//first couple of pages
elseif(($this->cur_page - $totalLinks) < 1)
{
$start = 1;
$end = $start + $totalLinks - 1;
}
// EDIT: END OF MODIFIED START AND END TO WORK HOW I WANT
// EDIT: CODEIGNITERS BASE PAGING SETUP SEE ABOVE FOR MY CHANGES
// $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;
// Is pagination being used over GET or POST? If get, add a per_page query
// string. If post, add a trailing slash to the base URL if needed
if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
{
$this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
}
else
{
$this->base_url = rtrim($this->base_url, '/') .'/';
}
// And here we go...
$output = '';
// Render the "First" link
// EDIT: CHANGED TO ALWAYS SHOW FIRST LINK AT LEAST
if ($this->first_link !== FALSE AND $this->cur_page != 1)
{
$first_url = ($this->first_url == '') ? $this->base_url."1" : $this->first_url;
$output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
}
else
{
$output .= $this->cur_tag_open.$this->first_link.$this->cur_tag_close;
}
// Render the "previous" link
// EDIT: CHANGED TO ALWAYS SHOW PREVIOUS LINK AT LEAST
if ($this->prev_link !== FALSE AND $this->cur_page != 1)
{
$i = $this->cur_page-1;
if ($i == 0 && $this->first_url != '')
{
$output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
}
else
{
$i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
$output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
}
}
else
{
$output .= $this->cur_tag_open.$this->prev_link.$this->cur_tag_close;
}
// EDIT: CHANGED THIS TO ALWAYS SHOW ALL LINKS WANTED EVEN IF ON FIRST PAGE
// Render the pages
if ($this->display_pages !== FALSE)
{
// Write the digit links
for ($loop = $start; $loop <= $end; $loop++)
{
// EDIT: DON'T NEED THIS THE WAY I'VE CHANGED IT
// $i = ($loop * $this->per_page) - $this->per_page;
if ($loop >= 0)
{
if ($this->cur_page == $loop)
{
$output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
}
else
{
$n = ($loop == 0) ? '0' : $loop;
if ($n == '' && $this->first_url != '')
{
$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
}
else
{
$n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
}
}
}
}
}
// Render the "next" link
// EDIT: CHANGED TO ALWAYS SHOW NEXT LINK AT LEAST
if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
{
$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page+1).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
}
else
{
$output .= $this->cur_tag_open.$this->next_link.$this->cur_tag_close;
}
// Render the "Last" link
// EDIT: CHANGED TO ALWAYS SHOW LAST LINK AT LEAST
if ($this->last_link !== FALSE AND $this->cur_page != $num_pages)
{
$i = (($num_pages));
$output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
}
else
{
$output .= $this->cur_tag_open.$this->last_link.$this->cur_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;
}
The only reason you get 1 2 3 > instead of « First < 1 2 3 4 5 > Last » is that you simply don't have enough rows in your result to generate more than 3 pages.
The "First" and "Last" links don't appear by default if you don't need them. It's not very clear from the documentation's example.
You won't get "Previous" links until you advance past page 1, the example is actually on page 3 (the "3" is bold).
Since you're providing your own templates in the configuration instead of using the defaults, your actual results will be slightly different.
If you want to do a quick test to see more links, just reduce your per_page to a lower number or include more rows in your total_rows. The total number of links shown can also be configured with num_links.
For those people who developing their CI app with PostgreSql and can't understand why pagination limit+offset works "weird":
Controller:
...
$offset = ($page-1)*$config["per_page"];
$this->reporting_model->some_fetch_method($id, $config["per_page"], $offset);
...
Model:
...
$this->db->limit($limit_perpage, $offset);
$this->db->where("id", $id);
$this->db->get('some_table');
...

php page navigation by serial number

Can anyone help in this php page navigation script switch on counting normal serial number? In this script there is a var called "page_id" - I want this var to store the real page link by order like 0, 1, 2, 3, 4, 5 ...
<?
$onpage = 10; // on page
/*
$pagerecord - display records per page
$activepage - current page
$records - total records
$rad - display links near current page (2 left + 2 right + current page = total 5)
*/
function navigation($pagerecord, $activepage){
$records = 55;
$rad = 4;
if($records<=$pagerecord) return;
$imax = (int)($records/$pagerecord);
if ($records%$pagerecord>0)$imax=$imax+1;
if($activepage == ''){
$for_start=$imax;
$activepage = $imax-1;
}
$next = $activepage - 1; if ($next<0){$next=0;}
$end =0;
$prev = $activepage + 1; if ($prev>=$imax){$prev=$imax-1;}
$start= $imax;
if($activepage >= 0){
$for_start = $activepage + $rad + 1;
if($for_start<$rad*2+1)$for_start = $rad*2+1;
if($for_start>=$imax){ $for_start=$imax; }
}
if($activepage < $imax-1){
$str .= ' <<< End <span style="color:#CCCCCC">•</span> < Forward | ';
}
$meter = $rad*2+1; //$rad; ---------------------
for($i=$for_start-1; $i>-1; $i--){
$meter--;
//$line = '|'; if ($meter=='0'){ $line = ''; }
$line = ''; if ($i>0)$line = '|';
if($i<>$activepage){
$str .= " <a href='?page=".$i."&page_id=xxx'>".($i)."</a> ".$line." ";
} else {
$str .= " <strong>[".($i)."]</strong> ".$line." ";
}
if($meter=='0'){ break; }
}
if($activepage > 0){
$str .= " | <a href='?page=".$next."'>Back ></a> <span style='color:#CCCCCC'>•</span> <a href='?page=".($end)."'>Start >>></a> ";
}
return $str;
}
if(is_numeric($_GET["page"])) $page = $_GET["page"];
$navigation = navigation($onpage, $page); // detect navigation
echo $navigation;
?>
Instead xxx here (page_id=xxx) I want to link to real page number by normal order when this script show links but reversed.
Really need help with this stuff! Thanks in advance!
I were helped by one of the programmers with my above script. So here is a worked example of the reversed page navigation on PHP.
<?
$onpage = 10; // on page
/*
$pagerecord - display records per page
$activepage - current page
$records - total records
$rad - display links near current page (2 left + 2 right + current page = total 5)
*/
function navigation($pagerecord, $activepage){
$records = 126;
$rad = 4;
if($records<=$pagerecord) return;
$imax = (int)($records/$pagerecord);
if ($records%$pagerecord>0)$imax=$imax+1;
if($activepage == ''){
$for_start=$imax;
$activepage = $imax-1;
}
$next = $activepage - 1; if ($next<0){$next=0;}
$end =0;
$prev = $activepage + 1; if ($prev>=$imax){$prev=$imax-1;}
$start= $imax;
if($activepage >= 0){
$for_start = $activepage + $rad + 1;
if($for_start<$rad*2+1)$for_start = $rad*2+1;
if($for_start>=$imax){ $for_start=$imax; }
}
$meter = $rad*2+1; //$rad; ---------------------
$new_meter = $for_start-1;
if($activepage < $imax-1){
$str .= ' <<< End <span style="color:#CCCCCC">•</span> < Forward | ';
}
for($i=$for_start-1; $i>-1; $i--){
$meter--;
//$new_meter++;
//$line = '|'; if ($meter=='0'){ $line = ''; }
$line = ''; if ($i>0)$line = '|';
if($i<>$activepage){
$str .= " <a href='?page=".$i."&page_id=".($imax-$i-1)."'>".($i)."</a> ".$line." ";
} else {
$str .= " <strong>[".($i)."]</strong> ".$line." ";
}
if($meter=='0'){ break; }
}
if($activepage > 0){
$str .= " | <a href='?page=".$next."&page_id=".($imax-$next-1)."'>Back ></a> <span style='color:#CCCCCC'>•</span> <a href='?page=".($end)."&page_id=".($start-1)."'>Start >>></a> ";
}
return $str;
}
if(is_numeric($_GET["page"])) $page = $_GET["page"];
$navigation = navigation($onpage, $page); // detect navigation
echo $navigation;
?>
$page = keeps the page number from the reversed order
$page_id = keeps the real page by serial order. so you can make SELECT queries to database and ORDER BY id DESC use.

Categories