Left Quote    If at first you don't succeed, redefine success.
- Anon.    
Right Quote
[login] | [Register]

Pagination with PHP

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

Getting Started

Since we will be interacting with the database, one of your first lines of code can be to connect to the database with your username and password. I recommend having this in a single file, and just include the file when you need to connect in your scripts. See below:

Now we will run a query to find out how many items (rows) match the category being viewed. This will be the total amount of items that can be showed.This query can look something like this:

$q is the query, and after that we make sure the query was successful. If not, we display the error. After that, $total_items will contain the value of the max amount of results. Next, we can declare a few of our important variables, $limit, $cat and $page. The values of these variables are obtained through the $_GET method.

If for any reason, this data is not available, we can set defaults. This might be because someone hand typed the url containing these values, or because someone is intentionally attempting to cause errors in your scripts. So, we use the code:

The first condition will set $limit to 10 (default) if $limit was empty, non numeric, less than 10, greater than 50. The function, is_numeric(); is boolean, so it returns true if $limit is numerical, false if it is non numerical (containing non numeric data). The second condition is basically the same. If $page is empty, non numeric, less than zero, greater than the max amount of results; Then set $page to the value of 1 (default).

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

7 Total Comments
4 Rating of 5 (1 Votes)


Tutorial Options

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



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

"" Copyright © 2002-2021; All rights lefted, all lefts righted.
Privacy Policy