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


Results per Page

We can allow the user to determine how many results per page he or she would like to see. To do this, we will provide a few links that will directly alter the value of $limit, this works because limit is in our query string ($limit = $_GET['limit']).

Here, we display the links to view different results per page:

See where we just alter the value of limit in the query string? This is also why we set the conditions above where $limit cannot be less than 10, or greater than 50.

The Results

Now we have all of our variables declared, and the query ran, so we are now at position to display our results. We will use a while loop to display the results, see the code below:

The code above will display each row matching the query we previously set. In the echo statement, you see result->title -This would display the title of the item in your table if you had a column named title. This is only an example, im sure the code you have to display each individual item is much larger and prettier, I just made it simple for you to see whats going on within the loop.

Displaying Prev., Next and pages in between

So, we've got our results matching the query displayed, now what about the rest? Believe it or not, the hard part is done and it should be smooth sailing from here. First, we will determine if we have to display the "Previous" link or not. Heres the code:

$prev_page: This contains the value of what will be our previous page, by taking the value of our current page, and subtracting 1. Next, we check if $prev_page is greater or equal to 1. If it is, we need to display the link -so we echo it. Take a look at the link we used:

http://www.yoursite.com/stuff/script.php?cat=$cat&limit=$limit&page=$prev_page

Notice in the url that we simply include the variables containing our current data. I would also like to take the time to say that this is a perfect case to use Mod_Rewrite for friendly URL's. Instead of having this long tedious query string, you can make the URL appear as simple as this:

http://www.yoursite.com/stuff/php/10/1.php

Where php is your $cat, 10 is $limit, and 1 is $page. Much, much better. And to possibly save you time, if you were to use this same exact format of url, your Mod_Rewrite code would be:



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...