Page 1 of 1

Pagination nesting problem

Posted: Tue Mar 28, 2017 5:55 pm
by rick.emmet
Hey Everyone,
I've been beating my head against the wall for two days on this one. I have a snippet of code that I have not touched for years – it used to run perfectly. I think I must have inadvertently changed the code at some point, and I can't get it to run properly.

It's a nesting problem in a series of “if statements” and no matter what, I can't seem to nest these conditionals so that they'll work. I'd like to use a switch statement, but I have five different arguments for it.

Here's the code:

Code: Select all

$pagination = "";
                                            if ($lastpage > 1) {
                                                $pagination .= "<div class=\"pagination\">";
                                                //previous button
                                                if ($page > 1) {
                                                    // When the condition is met, the code below runs and then drops down to "echo $pagination;"
                                                    $pagination.= "<a href=\"$targetpage?page=$prev\">< previous   </a>";
                                                } else {
                                                    $pagination.= "<span class=\"disabled\">< previous</span>";

                                                    //pages	
                                                    if ($lastpage < 7 + ($adjacents * 2)) { //not enough pages to bother breaking it up
                                                        for ($counter = 1; $counter <= $lastpage; $counter++) {
                                                            if ($counter == $page) {
                                                                $pagination.= "<span class=\"current\"> $counter </span>";
                                                            } else {
                                                                $pagination.= "<a href=\"$targetpage?page=$counter\"> $counter</a>";
                                                            } // END OF FIRST SERIRES OF IF STATEMENTS
                                                        }
                                                    } elseif ($lastpage > 5 + ($adjacents * 2)) { //enough pages to hide some
                                                        //close to beginning; only hide later pages
                                                        if ($page < 1 + ($adjacents * 2)) {
                                                            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
                                                                if ($counter == $page) {
                                                                    $pagination.= "<span class=\"current\"> $counter </span>";
                                                                } else {
                                                                    $pagination.= "<a href=\"$targetpage?page=$counter\"> $counter</a>";
                                                                }
                                                                $pagination.= "...";
                                                                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                                                                $pagination.= "<a href=\"$targetpage?page=$lastpage\"> $lastpage</a>";
                                                            }
                                                            //in middle; hide some front and some back
                                                        } elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
                                                            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                                                            $pagination.= "<a href=\"$targetpage?page=2\">2 </a>";
                                                            $pagination.= "...";
                                                            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
                                                                if ($counter == $page) {
                                                                    $pagination.= "<span class=\"current\"> $counter </span>";
                                                                } else {
                                                                    $pagination.= "<a href=\"$targetpage?page=$counter\"> $counter</a>";
                                                                }
                                                            }
                                                            $pagination.= "...";
                                                            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                                                            $pagination.= "<a href=\"$targetpage?page=$lastpage\"> $lastpage</a>";

                                                            //close to end; only hide early pages
                                                        } else {
                                                            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                                                            $pagination.= "<a href=\"$targetpage?page=2\">2 </a>";
                                                            $pagination.= "...";
                                                            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
                                                                if ($counter == $page) {
                                                                    $pagination.= "<span class=\"current\"> $counter </span>";
                                                                } else {
                                                                    $pagination.= "<a href=\"$targetpage?page=$counter\"> $counter</a>";
                                                                }
                                                            }
                                                        }
                                                    }

                                                    //next button
                                                    if ($page < $counter - 1) {
                                                        $pagination.= "<a href=\"$targetpage?page=$next\">   next ></a>";
                                                    } else {
                                                        $pagination.= "<span class=\"disabled\">next ></span>";
                                                        $pagination.= "</div>\n";
                                                    }
                                                }
                                            }
                                            echo $pagination;
My “do-while” loop (below the pagination) creates a set of small tables that display information for each record. On page 1, everything looks fine. On any other page, the pagination only displays the “previous” link. Once the condition “if ($page > 1)” is met the next line is executed, echoing the “previous” link, and the code drops to the bottom of the function. I've tried every other kind of logic I can think of and am all out of ideas. BTW, I'm using procedural PHP on this project. Any help will be very appreciated.
Cheers,
Rick

Re: Pagination nesting problem

Posted: Wed Mar 29, 2017 5:27 am
by Celauran
I don't see any sort of looping structure above this. Assuming $page is the current page, it's checking if it's greater than 1, echoing the "previous" link, and then ending because that's how if/else works. Is this all the code?

Re: Pagination nesting problem

Posted: Thu Mar 30, 2017 11:16 am
by rick.emmet
Hi Celauran,
Thank you for you're reply! No, it is not all the code. You're right, the $page variable is the current page, the code runs each time the page is refreshed. The code above is:

if ($page == 0)
$page = 1; //if no page var is given, default to 1.
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages / $limit);
$lpm1 = $lastpage - 1;

Somebody suggested that I un-nest the pagination, which I hadn't thought of. I'm going to give that a try. I believe that I never made a change to the function, it came from the forum, or a link someone provided to a tutorial. I'm wondering now how it ever worked??? Thanks again!
Cheers,
Rick

Re: Pagination nesting problem

Posted: Sun Apr 02, 2017 6:41 pm
by thinsoldier
I'd like to suggest un-spaghetti-fying it.

The logic for deciding if prev and next buttons should be shown should be closer together.

The logic for deciding if "1 2 ..." should be shown could be done once.

The logic for deciding which page numbers to show could add the page number to an array.

After all the logic listed above which detects and decides what should be shown, you'll be left with an array that you can loop over to actually do the showing. Yeah it might need a couple if-statements too but the result will be far easier to read and understand compared to what you have now.