Forum Moderators: coopster
I changed some code to add the "First" and "Last" options, but my question is how to limit the page results in between for a more advanced pagination like the example below?
Ex. First Prev 1 2 3 4 5 Next Last (where we know there are 75 pages for instance - don't display all 75 pages to be able to be clicked on just 5 or so at a time)
--My existing code--
// Build First Link
if($page > 1){
$prev = ($total_pages - $total_pages + 1);
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">First</a> ";
}
// Build Previous Link
if($page > 1){
$prev = ($page - 1);
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Prev</a> ";
}
for($i = 1; $i <= $total_pages; $i++){ // I imagine this is where the change would happen...after it increments
if(($page) == $i){
echo "<b>$i</b>";
} else {
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next</a> ";
}
// Build Last Link
if($page < $total_pages){
$next = ($page = $total_pages);
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Last</a><br><br> ";
}
For this project I don't anticipate that the results will be much at all, but I would like to know how to work with paging on a large scale like the 75 page example above.
Also, I noticed in my above code for the First and Prev links if I try to add the < symbol to the link it only shows as a < and the text is gone. This is odd because I can make the next> link like this and it displays fine. Thank you for any coding suggestions or examples, as I am sort of stumped on these few things.
Brad
[edited by: php4U at 8:26 pm (utc) on Nov. 15, 2007]
2) when you add the < sign that signals the begining of a html tag and therefor wil be evaluated.you will have to change to the equivalent html entity.
< should be changed to <
> should be changed to >
Thank you for your fast reply! I will work with your suggestions on the loop, to see what I can come up with. As far the < I clearly overlooked this, and it now displays correctly by using what you mentioned. What you said made since because once it saw < as the opening tag it didn't really effect > in the next and last links although I changed them to > anyway. Thanks agian for your help!
Changed code:
===============================================
for($i = $page; $i <= 5; $i++){
if(($page) == $i){
echo "<b>$i</b>";
} else {
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
===============================================
Changing this to $i = $page starts it at that page, but I am still missing something. I found many code examples to try to piece things together, but many don't pertain to what I am trying to do. Such as...
===============================================
for ($count = 1; $count <= 10; $count++) {
$square = $count * $count;
echo "$count squared is $square <br>";
}
===============================================
For instance, in my code on page 1 it shows 5 links
on page 2 it shows 4 links
<<First <Prev 2 3 4 5 Next> Last>>
on page 3 it shows 3 links
<<First <Prev 3 4 5 Next> Last>> and so on...
next will continue to increment and show those pages since that code didn't change, but after page 5 only <<First <Prev Next> Last>> is shown then the links re-appear one by one by clicking prev
with scenario A, its easier.
instead of
for($i = $page; $i <= 5; $i++){
if(($page) == $i){
echo "<b>$i</b>";
} else {
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
try
$MAX_LINKS = 5;
$counter = 0;
for($i = $page; $i <= $total_pages; $i++){
if(($page) == $i){
echo "<b>$i</b>";
}
elseif($counter < $MAX_LINKS) {
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
$counter++;
}
actualy that last bit is slow if you have a bunch of pages
try
$MAX_LINKS = 5;
for($i = 0; $i < $MAX_LINKS; $i++){
$newPage = $page + i;/*
if valid page
*/
if($newPage<=$total_pages){
/*
if current page
*/
if($newPage == $page){
echo "<b>$i</b>";
}
/*
not current page
*/
else{
echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$newPage\">$newPage</a> ";
}
}//ends if valid page
}//ends for
Thank you for the encouragement, and for my purpose your first portion of code will work since there won't be that many pages. This worked perfectly! Thank you very much for providing this code. I noticed that you mentioned the "counter" variable, but I wasn't sure how to implement it within my code.
With users in mind I have included a variable that will echo on this page so they can easily adjust in the config file how many results will display on each page $max_results_admin="10"; (will also help to limit the amount of total pages) Also, as a quick page reference I included Page ".$_GET['page']." of $total_pages so they can easily see where they are.
I did give the second code a try as well, and I may have to work with it a little bit to get it to display the links. At this point that isn't a big deal, but I will definitely take a closer look at it since you took the time to include it as well. I really appreciate your time and help on this.