Classic PHP Paging Code and Paging principle (1/3)

Source: Internet
Author: User

Classic PHP tutorial pagination code and page-splitting principle
1. Preface

Paging display is a very common way to browse and display large amounts of data, one of the most frequently handled events in Web programming. For the veteran of Web programming, writing this code is as natural as breathing, but for beginners, often on this issue does not feel a clue, so specially to write this article on this issue for detailed explanation, and strive to let the reading of this article after reading the principle of pagination and the realization of the method to understand. This article is for beginners to read, and all sample code is written using PHP.

2. Principle

The so-called pagination display, that is, the database tutorial in the result set for the artificial section of the show, here need two initial parameters:

How many records per page ($pagesize)?
What is the current page ($currentpageid)?

Now just give me one more result set, and I can show a specific piece of results.
As for other parameters, such as: the previous page ($previouspageid), the next page ($nextpageid), the total number of pages ($numpages), etc., can be based on these things in front.
In the MySQL tutorial database, for example, if you want to intercept a piece of content from a table, the SQL statement can be used: SELECT * FROM table limit offset, rows. Take a look at the following set of SQL statements and try to find the gauge in them.

First 10 records: SELECT * FROM table limit 0,10
11th to 20th record: SELECT * FROM table limit 10,10
21st to 30th Record: SELECT * FROM table limit 20,10
......

This set of SQL statements is actually a SQL statement that takes every page of data from the table when $pagesize=10, and we can summarize this template:

SELECT * FROM table limit ($currentpageid-1) * $pagesize, $pagesize

Take this template to the corresponding value and the top of the group of SQL statements in contrast to see if that is the case. The most important thing to do is to get the data, the rest is just passing the parameters, constructing the appropriate SQL statements and then using PHP to get the data from the database and display it. I'll explain this in concrete code.

3. Simple code
Please read the following code in detail, debug your own run once, it is best to modify it once, plus its own functions, such as search and so on.

<?php
Establishing a database connection
$link = mysql_connect ("localhost", "Mysql_user", "Mysql_password")
Or Die ("Could not connect:".) Mysql_error ());
Get the current number of pages
if (Isset ($_get[' page ')) {
$page = intval ($_get[' page '));
}
else{
$page = 1;
}
Number per page
$pagesize = 10;
Get total amount of data
$sql = "SELECT count (*) as amount from table";
$result = mysql_query ($sql);
$row = Mysql_fetch_row ($result);
$amount = $row [' Amount '];
Count how many pages there are in total
if ($amount) {
if ($amount < $page _size) {$page _count = 1;} If the total amount of data is less than $pagesize, then only one page
if ($amount% $page _size) {//Total data amount divided by the remainder of each page
$page _count = (int) ($amount/$page _size) + 1; If there are more than one, then the number of pages equals the total amount of data divided by the result of each page.
}else{
$page _count = $amount/$page _size; If there is no remainder, the number of pages equals the total amount of data divided by the results per page
}
}
else{
$page _count = 0;
}

Page links
$page _string = ';
if ($page = = 1) {
$page _string. = ' first page | previous page | ';
}
else{
$page _string. = ' <a href= '/?page=1>; the first page </a>|<a href= '/?page= '. ($page-1). ' > on a page </a>| ';
}
if ($page = = $page _count) | | ($page _count = 0)) {
$page _string. = ' next page | last ';
}
else{
$page _string. = ' <a href= '/?page= '. ($page + 1). ' > next page </a>|<a href= "/?page= '." $page _count. ' > Last </a> ';
}
Gets the data and returns the result in a two-dimensional array format
if ($amount) {
$sql = "SELECT * from table ORDER BY id desc limit". ($page-1) * $page _size. ", $page _size";
$result = mysql_query ($sql);

while ($row = Mysql_fetch_row ($result)) {
$rowset [] = $row;
}
}else{
$rowset = Array ();
}
There is no code to display the results, which is not in the scope of the discussion, as long as a foreach can be used to display the result with a two-dimensional array.
?>


4, OO style code
The database connection in the following code is handled using the Pear DB Class

<?php
filename:pager.class.php
Pagination class, which is used only to work with data structures, and is not responsible for working with the display
Class Pager
{
var $pagesize; Number of pages per page
var $currentpageid; Current number of pages
var $nextpageid; Next page
var $previouspageid; Previous page
var $numpages; Total pages
var $numitems; Total number of records
var $isfirstpage; is the first page
var $islastpage; is the last page
var $sql; SQL query Statement

Function Pager ($option)
{
Global $db;
$this->_setoptions ($option);
Total number of articles
if (!isset ($this->numitems))
{
$res = $db->query ($this->sql);
$this->numitems = $res->numrows ();
}
Total pages
if ($this->numitems > 0)
{
if ($this->numitems < $this->pagesize) {$this->numpages = 1;}
if ($this->numitems% $this->pagesize)
{
$this->numpages= (int) ($this->numitems/$this->pagesize) + 1;
}
Else
{
$this->numpages = $this->numitems/$this->pagesize;
}
}
Else
{
$this->numpages = 0;
}

Home 1 2 3 last

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.