Online Resources

eg: UK or Brides UK or Classical Art or Buy Music or Spirituality
 
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.
  • Business & Money
    • A Guide to Business
    • Guide to Finance
    • Ideas for Marketing
    • Legal Guide
    • Guide to Insurance
    • Lettre De Motivation
    • Guide to the Stock Market
    • Human Resource Career
    • Sales Marketing
    • Forex & Trading
    • Advertising & Marketing
    • Startup Guide
  • Technology
    • Guide to Technology
    • Cell Phones
    • Computer Software
    • IT Hardwares
    • Internet
    • Online Security
    • Cameras
    • Search Engine Optimization
    • Science & Technology
  • Women
    • Guide to Women
    • Relationship Advice
    • Marriage
    • Jewelry
    • Pregnancy
    • Fashion Style
    • Divorce Guide
    • Wedding Guide
    • Dating Guide
    • Natural Beauty
  • Health
    • Guide to Health
    • Guide to Medical
    • Plastic Surgery
    • Weight Loss
    • Sports
    • Body Wellness
    • Cancer Treatment
    • Common Illness
    • Health & Lifestyle
  • Education
    • Military Service
    • Politics and Policy
    • Arts & Humanities
    • Education and Teaching
    • Learn Languages
    • Colleges & Universities
  • Family
    • Quality Home Improvement
    • Hobbies and Interests
    • Family Guide to
    • Pet Guide
    • Loans Guide
    • Credit Cards
    • Gardening Guide
    • Home Security
    • Real Estate
    • Home Decor
    • Gift & Present
  • Travel
    • The Travel Guide
    • Adventure Travel
    • Cruise Ships
    • Beach Holiday
    • Travel Accommodation
    • Holiday Destinations
  • Cars
    • Information on Cars
    • Traffic Violations
    • Auto Insurance
    • Trailers
    • Sport Cars
    • The Bikes
  • Entertainment
    • Entertainment Guide
    • World Music
    • Photo & Video
    • Television & Games

Php Pagination With Mysql

    View: 
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
More Articles from
Assembly Jobs From Home
British American Insurance Company
Control Systems In Business
How To Earn Money On Internet
Will Young I Think I Better Leave Right Now
The Choice between Yes and Yes: A Psychological Revelation
"How To Deal With Freeloaders In Your Business
"How to Burn out Stress Instead of You!"
"Advanced Confidence Training" for Corporate Motivation
"Are you living your true "Authentic Self"?"
"Houston, we have contact." Attracting Clients at Expos!
"Feedback, thats all coaching really is." and other myths?
"...what Makes You Better?"
"He Hate Me": Turning Their Bad Attitude Into Your Great Results
Facing Angry Bears
How To Earn Money with your Membership Site?
How To Earn Money with your Membership Site on ecommerce ?
"21 Tips on How to Start a Home-Based Business "
"Bead-Dazzle:" Bead Makings Rich And Colorful History
» More on
  • Related Articles
  • Author
  • Most Popular
Mike Rogne has sinced written about articles on various topics from Cellulite. Mike is an experienced web developer with a wide range of programming capabilities. He has been delivering top quality web applications for several years primarily using Perl/CGI and PHP combined with MySQL. His ability to deliver high quality solutions w. Mike Rogne's top article generates over 720 views. Bookmark Mike Rogne to your Favourites.
1 2 3 Magic Parenting
Habits are hard to break. But, for the sake of your children you need to work on yourself, if you want something to change. Change yourself its easier than changing the other
 
A Guide to Business | Guide to Technology | Guide to Women | Guide to Health | Family Guide to | Travel & Vacations | Information on Cars

With over 20,000 authors and writers, we are a well known online resource and editorial services site in United Kingdom, Canada & America . Here, we cover all the major topics from self help guide to A Guide to Business, Guide to Finance, Ideas for Marketing, Legal Guide, Lettre De Motivation, Guide to Insurance, Guide to Health, Guide to Medical, Military Service, Guide to Women, Pet Guide, Politics and Policy , Guide to Technology, The Travel Guide, Information on Cars, Entertainment Guide, Family Guide to, Hobbies and Interests, Quality Home Improvement, Arts & Humanities and many more.
About Editorial Today | Contact Us | Terms of Use | Submit an Article | Our Authors