Pagination nesting problem
Posted: Tue Mar 28, 2017 5:55 pm
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:
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
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;
Cheers,
Rick