'Problem with showing pagination page numbers

I have a problem coding the numbers of pages of a pagination.

I will be as simple as possible in detailing what I need to do.

  • There are N numbers of pages (keep track as $listPages);
  • I need to show a maximum of 5 pages numbers generated as $listCount "1(current),2,3,4,5 + Next + Last" and keep the pattern with increasing pages (example for page 33 "First + Prev + 31,32,33 (current),34,35 + Next + Last");
  • $list keep track of the current page;

I written this code for the moment but it doesn't work correctly giving as output (example on page 7 "1,2,3,4,5,6,7(current),8,9 + Next + Last) it works going up, but it keep previous numbers. I hope some could help coding it, thanks!

            function ShowPageNumbers($pageUrl, $listPages, $list) {
                global $listPagesCount;
                foreach (range(1, $listPages) as $listCount) {
                    //Check if current page
                    if($listCount == ($list - 1)) {
                        echo "<li class='current' style='margin:3px;'>";
                        echo "<span class='pages-nav-item'>" . $listCount . "</span>";
                        echo "</li>";
                        $listPagesCount++;
                    } else {
                        if (($list - 1) == 1) {
                            //null
                        } else {
                            //echo "<li class='the-prev-page' style='margin:3px;'>";
                            //echo "<a class='pages-nav-item' href=" . $pageUrl . ($listCount + 4) . " title=" . ($listCount + 4) . ">«</a>";
                            //echo "</li>";
                        }
                        echo "<li style='margin:3px;'>";
                        echo "<a class='pages-nav-item' href=" . $pageUrl . $listCount . " title=" . $listCount . ">" . $listCount . "</a>";
                        echo "</li>";
                        $listPagesCount++;
                        if($listPagesCount >= 3 && $listPagesCount == $list + 1) {
                            echo "<li class='the-next-page' style='margin:3px;'>";
                            echo "<a class='pages-nav-item' href=" . $pageUrl . $listCount . " title=" . $listCount . ">»</a>";
                            echo "</li>";
                            echo "<li class='extend' style='margin:3px;'>";
                            echo "<span class='pages-nav-item'>...</span>";
                            echo "</li>";
                            echo "<li class='last-page first-last-pages' style='margin:3px;'>";
                            echo "<a class='pages-nav-item' href=" . $pageUrl . $listPages . " title='Ultimo'>Ultimo</a>";
                            echo "</li>";
                            break;
                        }
                    }
                }
            }
php


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source