Search
Left Quote    I have not failed. I've just found 10,000 ways that won't work.
- Thomas Alva Edison    
Right Quote
 
[login] | [Register]
 

Pagination with PHP

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

Continued...
Next, we will set two more variables. These will be the total number of pages needed, and the limit we set to use in our query.


$total_pages: in this variable, we get the total number of pages needed. We use the ceil(); function - This will round up any remainder we have from the calculation within. We need it to round up, so that no items are left out. So, if $total_items were 27, and $limit was 10...that would return : 2.7 - If we didnt use ceil(); you cant have .7 of a page, and if we showed 2 pages, we would lose those .7 results. So, we round that up to 3 with ceil(); and in this case = 2 pages of 10, and 1 page of 7.

$set_limit: This will contain the value of where we will start, or where we left off, depending on the value of $page and $limit. If $page was 3 and limit was $10 then the variable will contain the value: 20. And that would be the value we use in out query for LIMIT.

The Query


The most important thing about pagination is the query. The part of our query that makes pagination possible is LIMIT. There are two ways to use LIMIT.

Example #1: SELECT `stuff` FROM `table` LIMIT 5
In the example above, the query will return the first 5 rows.

Example #2: SELECT `stuff` FROM `table` LIMIT 5, 10
In this example, the query will return 10 rows, beginning with row 5.

We will be using LIMIT with the #2 example; except where you see 5 and 10 above, we will place the variables, $set_limit and $limit.
Check out our query below:



This is the heart of our pagination, each time the query will return different results, depending on the value of $set_limit (which changes depending on the value of $page) and what our limit is set to in $limit. Now, we'll break down the code:

$q: We're selecting all data from the table where category of the table matches the category we're in, starting with row equaling value of $set_limit, limiting the results to follow only to the value of $limit. After that, we check that the query was successful, and display an error if it was not.

$err: Contains the number of rows returned from the query, if that number is zero, we indicate that no matches were found.



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,396 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