We all know that CodeIgniter (CI) has a paging class library, and now we create an AJAX paging that we need based on its native class library.
First in the/application/libraries/directory to create a my_pagination.php, the code is as follows:
<?php/** *417 Add is primarily to implement Ajax paging **/class My_pagination extends Public function ci_pagination{() {
Parent::__construct (); }/** * Generate the pagination links * * @access public * @return string/function Create_ajax_li
Nks () {//If we item count or Per-page Total are 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 ';
}//Set The base page index for starting page number if ($this->use_page_numbers) {$base _page = 1;
else {$base _page = 0;
}//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)!= $bas
E_page) {$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)!= $base _page) {$this->c
Ur_page = $CI->uri->segment ($this->uri_segment);
Prep the current page-no funny business!
$this->cur_page = (int) $this->cur_page; }//Set current page to 1 if using page numbers instead of offset if ($this->use_page_numbers and $
This->cur_page = = 0) {$this->cur_page = $base _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 = $base _page;
}//is the page number beyond the result range?
If so we show the last page if ($this->use_page_numbers) {if ($this->cur_page > $num _pages)
{$this->cur_page = $num _pages; } else {if ($this->cur_page > $this->total_rows) {$this->cur_page = ($num _
PAGES-1) * $this->per_page;
}} $uri _page_number = $this->cur_page;
if (! $this->use_page_numbers) {$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->nu M_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 O R $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 we go ... $output = ';
Render the "I" 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 '. "onclick= ' ajax_page (0); return false; '". $this->anchor_class. ' href= ' javascript:void (0) ">". $this->first_link. '
</a> '. $this->first_tag_close; }//Render the Previous "link if ($this->prev_link!== FALSE and $this->cur_page!= 1) {if ($this->use_page_numbers
) {$i = $uri _page_number-1;
else {$i = $uri _page_number-$this->per_page; } if ($i = = 0 && $this->first_url!= ') {$output. = $this->prev_tag_open. <a '. "onclick= ' ajax_page (0); return false; '". $this->anchor_class. ' href= ' javascript:void (0) ">". $this->prev_link. '
</a> '. $this->prev_tag_close; else {$i = ($i = = 0)?
": $this->prefix. $i $this->suffix; $output. = $this->prev_tag_open. <a '. "Onclick= ' Ajax_page ({$i}); return false; '". $this->anchor_class. ' href= ' javascript:void (0) ">". $this->prev_link. '
</a> '. $this->prev_tag_close; }//Render the pages if ($this->display_pages!== FALSE) {//Write the digit links fo R ($loop = $start-1; $loop <= $end; $loop++) {if ($this->use_page_numbers) {$i = $loop;
else {$i = ($loop * $this->per_page)-$this->per_page; } if ($i >= $base _page) {if ($this->cur_page = = $loop) {$output. = $this->cur_tag_open. $loop. $this->cur_tag_close; Current Page} else {$n = ($i = = $base _page)?
': $i; if ($n = = ' && $this->first_url!= ') {$output. = $this->num_tag_open. <a '. "onclick= ' ajax_page (0); return false; '". $this->anchor_class. ' href= ' javascript:void (0) ">". $loop. '
</a> '. $this->num_tag_close; else {$n = ($n = = ")?
": $this->prefix. $n $this->suffix; $output. = $this->num_tag_open. <a '. "Onclick= ' Ajax_page ({$n}); return false; '". $this->anchor_class. ' href= ' jaVascript:void (0) ">". $loop. '
</a> '. $this->num_tag_close; }}///Render the "Next" link if ($this->next_link!== FALSE and $this->c
Ur_page < $num _pages) {if ($this->use_page_numbers) {$i = $this->cur_page + 1;
else {$i = ($this->cur_page * $this->per_page);
$ajax _p = $this->prefix. $i. $this->suffix; $output. = $this->next_tag_open. <a '. "Onclick= ' Ajax_page ({$ajax _p}); return false; '". $this->anchor_class. ' href= ' javascript:void (0) ">". $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) {if ($this->use_page_numbers) {$i = $num _pages;
} else {