Skip to content

Commit 5784c34

Browse files
committed
🔨 improve pagination class
Signed-off-by: otengkwame <developerkwame@gmail.com>
1 parent 2df2794 commit 5784c34

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

CodeIgniter/Framework/libraries/Pagination.php

+51-12
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ public function create_links()
514514
{
515515
$this->cur_page = $this->CI->input->get($this->query_string_segment);
516516
}
517-
elseif (empty($this->cur_page))
517+
else
518518
{
519519
// Default to the last segment number if one hasn't been defined.
520520
if ($this->uri_segment === 0)
@@ -530,10 +530,6 @@ public function create_links()
530530
$this->cur_page = str_replace([$this->prefix, $this->suffix], '', $this->cur_page);
531531
}
532532
}
533-
else
534-
{
535-
$this->cur_page = (string) $this->cur_page;
536-
}
537533

538534
// If something isn't quite right, back to the default base page.
539535
// A fix for this is below [if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0))
@@ -593,7 +589,7 @@ public function create_links()
593589
{
594590
$i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
595591

596-
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, ($this->cur_page - 1));
592+
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
597593

598594
if ($i === $base_page)
599595
{
@@ -613,12 +609,47 @@ public function create_links()
613609
// Render the pages
614610
if ($this->display_pages !== false)
615611
{
612+
/*
613+
* On page 4, show hidden page 2
614+
*/
615+
if ($start == 4) {
616+
$start--;
617+
}
618+
619+
/*
620+
* On pages after 4, show dots before pages
621+
*/
622+
if (($start) > 4) {
623+
$output .= '<div class="continues">. . .</div>';
624+
}
625+
626+
/*
627+
* On page 4th last, show hidden page 2nd last
628+
*/
629+
if ($start == ($num_pages - 3) && $start > 4) {
630+
$end++;
631+
}
632+
633+
/*
634+
* On last page, show hidden page 3rd last
635+
*/
636+
if ($start == $num_pages && $start> 3 ) {
637+
$start--;
638+
}
639+
640+
/*
641+
* On page 1, show hidden page 3
642+
*/
643+
if ($start == 1 && $end < $num_pages-1) {
644+
$end++;
645+
}
646+
616647
// Write the digit links
617648
for ($loop = $start - 1; $loop <= $end; $loop++)
618649
{
619650
$i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
620651

621-
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop);
652+
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
622653

623654
if ($i >= $base_page)
624655
{
@@ -636,19 +667,28 @@ public function create_links()
636667
else
637668
{
638669
$append = $this->prefix.$i.$this->suffix;
639-
$output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.'>'
670+
$output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.$this->_attr_rel('start').'>'
640671
.$loop.'</a>'.$this->num_tag_close;
641672
}
673+
642674
}
643675
}
676+
677+
/*
678+
* On pages before last, show dots
679+
*/
680+
if ($end < ($num_pages-1)) {
681+
$output .= '<div class="continues">. . .</div>';
682+
}
683+
644684
}
645685

646686
// Render the "next" link
647687
if ($this->next_link !== false && $this->cur_page < $num_pages)
648688
{
649689
$i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
650690

651-
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1);
691+
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
652692

653693
$output .= $this->next_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
654694
.$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
@@ -662,13 +702,12 @@ public function create_links()
662702

663703
}
664704

665-
666-
// Render the "Last" link
705+
// Render the "last" link
667706
if ($this->last_link !== false && ($this->cur_page + $this->num_links + ! $this->num_links) < $num_pages)
668707
{
669708
$i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
670709

671-
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages);
710+
$attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
672711

673712
$output .= $this->last_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>'
674713
.$this->last_link.'</a>'.$this->last_tag_close;

0 commit comments

Comments
 (0)