Jump to content

[SOLVED] PHP Paging (like that at the bottom of the php help page)


Alexhoward

Recommended Posts

Good Morning,

 

I have been looking for examples of how to display paging like that at the bottom of the php help page.

 

Where it shows a number of results then "....." then the last page

 

and when you move on it shows "1...11,12,13,14...285"

 

could anyone point me to a good tutorial?

 

i am pulling the results from MySQL

 

Thanks in advance

Link to comment
Share on other sites

i use this formula for my users page :)

 

$prev_page = $users - 1;


							echo "<tr><td colspan='3'>";

									if($prev_page >= 1){
										echo "<b><< <a href=users.php?users=$prev_page>Prev.</a></b>"; //if you are on page 2 or more display a previous  link
									}
								if($totalpages <= 3){
									//this is the pagination link										 
									for($i = 1; $i <= $totalpages; $i++){ 
										echo "<a href='users.php?users=$i'> $i </a>| ";//if there are less than 3 pages just show 1|2|3
									}

								}else{
									$twobefore = $users-2;
									$onebefore = $users-1;
									$oneafter = $users+1;
									$twoafter = $users+2;
									//i prefer using a nested version as i understand it easuier you could just as easily use an elseif formula
									if($twobefore >= 1){
										echo "<a href=users.php?users=1> 1 </a>";
										echo "<a href=users.php?users=$twobefore> $twobefore </a>";//if you are on page 3 or more show a link to the previous 2 pages
										if($onebefore >= 1){
											echo "<a href=users.php?users=$onebefore> $onebefore </a>";//if you are on page 2 or more show a link to the previous page
											echo "<a href=users.php?users=$user> $user </a>"; // show the link for the page you are currently on 
											if($oneafter <= $totalpages){
												echo "<a href=users.php?users=$oneafter> $oneafter </a>";// if you are more than 1 page off max pages show link to next page
												if($twoafter <= $totalpages){
													echo "<a href=users.php?users=$twoafter> $twoafter </a>";// if you are more than 2 page off max pages show link to the next 2 pages
													if($twoafter < totalpages){
														echo "...<a href=users.php?users=$totalpages> $totalpages </a>";//show a link to the last page
													}
												}
											}
										}
									}

									$next_page = $users + 1;
									if($next_page <= $totalpages) {
									   echo"<a href=users.php?users=$next_page><b>Next</b></a> <b>>></b>";//next link										   
									}
							}

Link to comment
Share on other sites

You aren't really going to find a tutorial that explains how to make the navbar exactly as you want it, as that's a matter of style more than anything, and not really related to pagination principles, anymore than say, making the columns sortable.  It all comes down to a loop from 1 to total rows, and throwing in conditions to make it display how you want it to display.

 

Basic Pagination

 

It makes the pagination bar look like this:

 

<< < 5 6 7 [8] 9 10 11 > >>

 

the < > are prev/next and the << >> are first/last.  All you really have to change is having it echo  "...." and 1/$numrows instead of arrows.

Link to comment
Share on other sites

  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.