Search
Left Quote    Maybe this world is another planet's Hell.
- Aldous Huxley    
Right Quote
 
[login] | [Register]
 

Pagination with PHP

by: bs0d
Page: 4 of 6
(View All)

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:



1  |  2  |  3  |  4  |  5  |  6  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

152,414 Views
7 Total Comments
4 Rating of 5 (1 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank