Online Resources
eg:
UK
or
Brides UK
or
Classical Art
or
Buy Music
or
Spirituality
Toggle navigation
eg:
UK
or
Brides UK
or
Classical Art
or
Buy Music
or
Spirituality
Business & Money
Technology
Women
Health
Education
Family
Travel
Cars
Entertainment
Featured Sites
SD Editorials
Online Guide and article directory site.
Foodeditorials.com
Over 15,000 recipes & editorials on food.
Lyricadvisor.com
Get 100,000 Lyric & Albums.
Video on Php Pagination With Mysql
View:
Similar Videos
Videos on Assembly Jobs From Home
Videos on British American Insurance Company
Videos on Control Systems In Business
Videos on How To Earn Money On Internet
Videos on Will Young I Think I Better Leave Right Now
Videos on
Videos on The Choice between Yes and Yes
:
A Psychological Revelation
Videos on "How To Deal With Freeloaders In Your Business
Videos on "How to Burn out Stress Instead of You
!
"
Videos on "Advanced Confidence Training" for Corporate Motivation
Videos on "Are you living your true "Authentic Self"
?
"
Videos on "Houston
,
we have contact
.
" Attracting Clients at Expos
!
Videos on "Feedback
,
thats all coaching really is
.
" and other myths
?
Videos on "
...
what Makes You Better
?
"
Videos on "He Hate Me"
:
Turning Their Bad Attitude Into Your Great Results
Videos on Facing Angry Bears
Videos on
!
How To Earn Money with your Membership Site
?
Videos on
!
How To Earn Money with your Membership Site on ecommerce
?
Videos on "21 Tips on How to Start a Home
-
Based Business "
Videos on "Bead
-
Dazzle
:
" Bead Makings Rich And Colorful History
Currently No Video Available
Php Pagination With Mysql
Mike Rogne
In order to creating your own paging system, you will need to get some information first.
You will need the total number of list entries, number of results per page, current page number being viewed,
the total number of pages and the offset.
Lets start with the total number of list entries. In order to do this, we need some test data in a MySQL table.
For demonstration purposes, we are going to use the following MySQL table:
CREATE TABLE products (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(50) NOT NULL,
description TEXT NOT NULL
);
INSERT INTO products (name,description) VALUES ('Product 1','Description for Product 1');
INSERT INTO products (name,description) VALUES ('Product 2','Description for Product 2');
INSERT INTO products (name,description) VALUES ('Product 3','Description for Product 3');
INSERT INTO products (name,description) VALUES ('Product 4','Description for Product 4');
INSERT INTO products (name,description) VALUES ('Product 5','Description for Product 5');
INSERT INTO products (name,description) VALUES ('Product 6','Description for Product 6');
INSERT INTO products (name,description) VALUES ('Product 7','Description for Product 7');
INSERT INTO products (name,description) VALUES ('Product 8','Description for Product 8');
INSERT INTO products (name,description) VALUES ('Product 9','Description for Product 9');
INSERT INTO products (name,description) VALUES ('Product 10','Description for Product 10');
Now that we have a MySQL table full of "fake" products, lets get the paging working!
First, we need the total number of list entries. To do this, we will use MySQL's COUNT() function.
$result = mysql_query("SELECT COUNT(*) AS total_entries FROM products") or die(mysql_error());
$row = mysql_fetch_row($result);
$total_entries = $row[0];
Now we have the total entries retrieved and stored in the $total_entries variable.
Next, we need to get the number of results per page. It is a common programming practice when developing scripts to use a central
configuration file (such as config.php). For the sake of simplicity, we'll call our variable $entries_per_page.
$entries_per_page = 3;
Next, we need to get the current page number being viewed. We will call our page number variable $page_number.
if(isset($_GET['page_number'])) {
$page_number = $_GET['page_number'];
} else {
$page_number = 1;
}
This means set $page_number to the page_number specified via query string if it exists, otherwise set it to page number 1.
Next, we need to get the total number of pages that there will be.
An important thing to remember is that even if the very last page contains only one product, that one product will still be one page.
To do this, we use PHP's ceil() function. This means round up the number to the nearest integer.
For example, ceil(2.1) will round up to 3. ceil(2.9) will round up to 3.
In order to figure out the total number of pages, you simply do ceil(TOTAL ENTRIES / ENTRIES PER PAGE).
$total_pages = ceil($total_entries / $entries_per_page);
For this specific example, $total_pages has the value 4.
Finally, we need to determine the offset. What exactly is the "offset"? The offset is the number of the first entry to pull.
It is what you use to tell MySQL where to start fetching data. For example, if we are viewing products on page two, we would
want to return only products 4 through 7. If we are viewing products on page three, we would want to return only products 8 through 10.
To get an offset, you simply do: (CURRENT PAGE - 1) * ENTRIES PER PAGE
$offset = ($page_number - 1) * $entries_per_page;
Now that we have all of the data we require we are able to use MySQL to pull all products within our "page" being viewed.
$result = mysql_query("SELECT * FROM products LIMIT $offset, $entries_per_page") or die(mysql_error());
while($obj = mysql_fetch_object($result)) {
// Display the data however you want here.
print
Next Paragraph..
A Guide to Business
|
Guide to Technology
|
Guide to Women
|
Guide to Health
|
Family Guide to
|
Travel & Vacations
|
Information on Cars