PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Pagination with PHP
Your Ad Here

Pagination with PHP


Pages in between

Getting back on track, we will now display the pages in between (not previous or next). To do so, we will use a for loop. View the code below:

This is a basic for loop structure, where it will loop until it is less than or equal to the value of $total_pages. Inside the for loop, we set a condition that if $a equals the current page, to just echo that value, not linked (because we're already on that page). If not, echo the rest of the pages, linked.

Next Page

Displaying the next page link is identical to displaying the previous page link. Your code should look a bit like this:

Set the value of next page in the $next_page variable by taking the value of the current page ($page) and adding 1. Now, we have the condition that will be executed and display the back link only if $next_page is less than or equal to the value of $total_pages. That is all for displaying the paginated links.

FYI:

I would like to bring important information to your attention. If your category names contain spaces, like, "visual basic" when you try to use that in your link, it will not link properly. Because, it will only read until the space is encountered, and then stops. Your link would read, "visual" instead. Therefore, we will use the function, urlencode(); to replace those spaces so that the browser can interpret them. See the code below,

Simple as that. Now, if $cat was, "visual basic" it is now "visual+basic" and the links will be valid, and browser friendly. Just be sure to place this line of code before the code where you begin to display the links.

Complete Code

Now that we have covered though the entire script, piece by piece- follow the link below to view the entire code:

Pagination Code

Also, keep in mind that if you just copy and paste this script, it will _not_ work. You need to edit the script to meet your needs, like the table name, link paths, database connection, -pretty much everything. But I put in comments where changes need made.

Conclusion

I hope this tutorial meets your needs. If you have any questions on this script, feel free to ask in the forums. I am sure there are millions of ways to accomplish various actions in the code, but this is the way I chose to do it, which does work. If you would like to see a working example of this code, click this link:

Working Example

Until next time, happy coding~


Author's URL: allsyntax.com
Thank you for voting. Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "Pagination with PHP"

Only registered users can write comment

No comments yet...