Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the code to override the built in pagination class. Place it in application/libraries and just do everything else like normal.</p> <p>I've added a comment to each line I edited from the original class.</p> <p>NOTE: in your sample code you put total pages in the total_rows config value. Be sure to put the total number of records, not the total number of pages.</p> <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Pagination extends CI_Pagination { function create_links() { // If our item count or per-page total is zero there is no need to continue. if ($this-&gt;total_rows == 0 OR $this-&gt;per_page == 0) { return ''; } // Calculate the total number of pages $num_pages = ceil($this-&gt;total_rows / $this-&gt;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 =&amp; get_instance(); if ($CI-&gt;config-&gt;item('enable_query_strings') === TRUE OR $this-&gt;page_query_string === TRUE) { if ($CI-&gt;input-&gt;get($this-&gt;query_string_segment) != 0) { $this-&gt;cur_page = $CI-&gt;input-&gt;get($this-&gt;query_string_segment); // Prep the current page - no funny business! $this-&gt;cur_page = (int) $this-&gt;cur_page; } } else { if ($CI-&gt;uri-&gt;segment($this-&gt;uri_segment) != 0) { $this-&gt;cur_page = $CI-&gt;uri-&gt;segment($this-&gt;uri_segment); // Prep the current page - no funny business! $this-&gt;cur_page = (int) $this-&gt;cur_page; } } $this-&gt;num_links = (int)$this-&gt;num_links; if ($this-&gt;num_links &lt; 1) { show_error('Your number of links must be a positive number.'); } if ( ! is_numeric($this-&gt;cur_page) || $this-&gt;cur_page &lt; 1) // EDITED { $this-&gt;cur_page = 1; // EDITED } // Is the page number beyond the result range? // If so we show the last page if (($this-&gt;cur_page * $this-&gt;per_page) &gt; $this-&gt;total_rows) // EDITED { $this-&gt;cur_page = ($num_pages - 1); // EDITED } $uri_page_number = $this-&gt;cur_page; //$this-&gt;cur_page = floor(($this-&gt;cur_page/$this-&gt;per_page) + 1); // EDITED // Calculate the start and end numbers. These determine // which number to start and end the digit links with $start = (($this-&gt;cur_page - $this-&gt;num_links) &gt; 0) ? $this-&gt;cur_page - ($this-&gt;num_links - 1) : 1; $end = (($this-&gt;cur_page + $this-&gt;num_links) &lt; $num_pages) ? $this-&gt;cur_page + $this-&gt;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-&gt;config-&gt;item('enable_query_strings') === TRUE OR $this-&gt;page_query_string === TRUE) { $this-&gt;base_url = rtrim($this-&gt;base_url).'&amp;amp;'.$this-&gt;query_string_segment.'='; } else { $this-&gt;base_url = rtrim($this-&gt;base_url, '/') .'/'; } // And here we go... $output = ''; // Render the "First" link if ($this-&gt;first_link !== FALSE AND $this-&gt;cur_page &gt; ($this-&gt;num_links)) // EDITED { $first_url = ($this-&gt;first_url == '') ? $this-&gt;base_url : $this-&gt;first_url; $output .= $this-&gt;first_tag_open.'&lt;a '.$this-&gt;anchor_class.'href="'.$first_url.'"&gt;'.$this-&gt;first_link.'&lt;/a&gt;'.$this-&gt;first_tag_close; } // Render the "previous" link if ($this-&gt;prev_link !== FALSE AND $this-&gt;cur_page != 1) { $i = $this-&gt;cur_page-1; // EDITED if ($i == 1 &amp;&amp; $this-&gt;first_url != '') // EDITED { $output .= $this-&gt;prev_tag_open.'&lt;a '.$this-&gt;anchor_class.'href="'.$this-&gt;first_url.'"&gt;'.$this-&gt;prev_link.'&lt;/a&gt;'.$this-&gt;prev_tag_close; } else { $i = ($i == 1) ? '' : $this-&gt;prefix.$i.$this-&gt;suffix; // EDITED $output .= $this-&gt;prev_tag_open.'&lt;a '.$this-&gt;anchor_class.'href="'.$this-&gt;base_url.$i.'"&gt;'.$this-&gt;prev_link.'&lt;/a&gt;'.$this-&gt;prev_tag_close; } } // Render the pages if ($this-&gt;display_pages !== FALSE) { // Write the digit links for ($loop = $start; $loop &lt;= $end; $loop++) { $i = $loop; // EDITED if ($i &gt;= 0) { if ($this-&gt;cur_page == $loop) { $output .= $this-&gt;cur_tag_open.$loop.$this-&gt;cur_tag_close; // Current page } else { $n = ($i == 0) ? '' : $i; if ($n == '' &amp;&amp; $this-&gt;first_url != '') { $output .= $this-&gt;num_tag_open.'&lt;a '.$this-&gt;anchor_class.'href="'.$this-&gt;first_url.'"&gt;'.$loop.'&lt;/a&gt;'.$this-&gt;num_tag_close; } else { $n = ($n == '') ? '' : $this-&gt;prefix.$n.$this-&gt;suffix; $output .= $this-&gt;num_tag_open.'&lt;a '.$this-&gt;anchor_class.'href="'.$this-&gt;base_url.$n.'"&gt;'.$loop.'&lt;/a&gt;'.$this-&gt;num_tag_close; } } } } } // Render the "next" link if ($this-&gt;next_link !== FALSE AND $this-&gt;cur_page &lt; $num_pages) { $output .= $this-&gt;next_tag_open.'&lt;a '.$this-&gt;anchor_class.'href="'.$this-&gt;base_url.$this-&gt;prefix.($this-&gt;cur_page+1).$this-&gt;suffix.'"&gt;'.$this-&gt;next_link.'&lt;/a&gt;'.$this-&gt;next_tag_close; // EDITED } // Render the "Last" link if ($this-&gt;last_link !== FALSE AND ($this-&gt;cur_page + $this-&gt;num_links) &lt; $num_pages) { $i = $num_pages; // EDITED $output .= $this-&gt;last_tag_open.'&lt;a '.$this-&gt;anchor_class.'href="'.$this-&gt;base_url.$this-&gt;prefix.$i.$this-&gt;suffix.'"&gt;'.$this-&gt;last_link.'&lt;/a&gt;'.$this-&gt;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-&gt;full_tag_open.$output.$this-&gt;full_tag_close; return $output; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload