PHP _php tutorial with page and page pagination classes

Source: Internet
Author: User
The so-called paging display, that is, the result set in the database is artificially divided into a section of the display, here need two initial parameters: how many records per page ($PageSize)?

What is the current page ($CurrentPageID)?
Now just give me a result set and I can show a specific result.
For other parameters, such as the previous page ($PReviousPageID), the next page ($NextPageID), the total number of pages ($numPages), and so on, can be based on the front of these things.
For example, in MySQL database, if you want to intercept a piece of content from within 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 out the rate of compliance.
Top 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 in the table when $pagesize=10, and we can summarize a template like this:
SELECT * FROM Table Limit ($CurrentPageID – 1) * $PageSize, $PageSize
Take this template and replace the corresponding value with the set of SQL statements above to see if that's the case. Take care of the most important question of how to get the data, all that is left is to pass the parameters, construct the appropriate SQL statement, and then use PHP to retrieve the data from the database and display it. I'll use the specific code to illustrate the following.
3. Simple code
Please read the following code in detail, run your own debugging once, it is best to modify it once, plus your own features, such as search and so on.
[PHP]

The code is as follows Copy Code

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;
}
Quantity per page
$PageSize = 10;
Get Total data volume
$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 data volume is less than $pagesize, then there is only one page
if ($amount% $page _size) {//Take the total amount of data divided by the remainder of each page
$page _count = (int) ($amount/$page _size) + 1; If there is an excess, the number of pages equals the total amount of data divided by the result of each page plus one
}else{
$page _count = $amount/$page _size; If there is no remainder, the number of pages equals the total data amount divided by the result of each page
}
}
else{
$page _count = 0;
}
Page link
$page _string = ";
if ($page = = 1) {
$page _string. = ' first page | prev | ';
}
else{
$page _string. = ' first page | prev | ';
}
if ($page = = $page _count) | | ($page _count = = 0)) {
$page _string. = ' next page | last ';
}
else{
$page _string. = ' next page | last ';
}
Get data to return results in 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, that is not the scope of the discussion, as long as a foreach can be used to easily use the resulting two-dimensional array to display the results
?>

PHP page maker, auto-component Surface page number, JS call function

The code is as follows Copy Code

Class pageview{
/** page **/
public $pageNo = 1;
/** Page Size **/
Public $pageSize = 20;
/** total number of pages **/
Public $pageCount = 0;
/** total number of records **/
Public $totalNum = 0;
/** offset, current page start Row **/
Public $offSet = 0;
/** data per page **/
Public $pageData = Array ();

/** whether there is a previous page **/
Public $hasPrePage = true;
/** whether there is a next page **/
Public $hasNextPage = true;

Public $pageNoList = Array ();

Public $jsFunction = ' jsfunction ';
/**
*
* @param unknown_type $count total number of rows
* @param unknown_type $size Paging size
* @param unknown_type $string
*/
Public function __construct ($count =0, $size =20, $pageNo =1, $pageData =array (), $jsFunction = ' jsfunction ') {

$this->totalnum = $count;//Total Record Count
$this->pagesize = $size;//Size per page
$this->pageno = $pageNo;
Calculate Total Pages
$this->pagecount = ceil ($this->totalnum/$this->pagesize);
$this->pagecount = ($this->pagecount<=0)? 1: $this->pagecount;
Check PageNo
$this->pageno = $this->pageno = = 0? 1: $this->pageno;
$this->pageno = $this->pageno > $this->pagecount? $this->pagecount: $this->pageno;

Calculate offset
$this->offset = ($this->pageno-1) * $this->pagesize;
Calculate if there is a previous page next page
$this->hasprepage = $this->pageno = = 1? false:true;

$this->hasnextpage = $this->pageno >= $this->pagecount? false:true;

$this->pagedata = $pageData;
$this->jsfunction = $jsFunction;

}
/**
* Paging algorithm
* @return
*/
Private Function Generatepagelist () {
$pageList = Array ();
if ($this->pagecount <= 9) {
for ($i =0; $i < $this->pagecount; $i +) {
Array_push ($pageList, $i + 1);
}
}else{
if ($this->pageno <= 4) {
for ($i =0; $i <5; $i + +) {
Array_push ($pageList, $i + 1);
}
Array_push ($pageList,-1);
Array_push ($pageList, $this->pagecount);

}else if ($this->pageno > $this->pagecount-4) {
Array_push ($pageList, 1);

Array_push ($pageList,-1);
for ($i =5; $i >0; $i-) {
Array_push ($pageList, $this->pagecount-$i + 1);
}
}else if ($this->pageno > 4 && $this->pageno <= $this->pagecount-4) {
Array_push ($pageList, 1);
Array_push ($pageList,-1);

Array_push ($pageList, $this->pageno-2);
Array_push ($pageList, $this->pageno-1);
Array_push ($pageList, $this->pageno);
Array_push ($pageList, $this->pageno + 1);
Array_push ($pageList, $this->pageno + 2);

Array_push ($pageList,-1);
Array_push ($pageList, $this->pagecount);

}
}
return $pageList;
}

/***
* Create a paging control
* @param
* @return String
*/
Public Function Echopageasdiv () {
$pageList = $this->generatepagelist ();

$pageString = "";

if (!empty ($pageList)) {
if ($this->pagecount >1) {
if ($this->hasprepage) {
$pageString = $pageString. " Jsfunction. "(" . ($this->pageno-1). ")" > Previous page ";
}
foreach ($pageList as $k = = $p) {
if ($this->pageno = = $p) {
$pageString = $pageString. " " . $this->pageno. "";
Continue
}
if ($p = =-1) {
$pageString = $pageString. " ...";
Continue
}
$pageString = $pageString. " Jsfunction. "(" . $p. ")" > ". $p. "";
}

if ($this->hasnextpage) {
$pageString = $pageString. " Jsfunction. "(" . ($this->pageno+1). ")" > Next page ";
}

}
}
$pageString = $pageString. ("");
return $pageString;
}
}

?>

PHP calls

The code is as follows Copy Code


$pageNo = $_get[' PageNo ');
if (empty ($pageNo)) {
}
Paging data
$pageData = News::getnewspage ($pageNo, $pageSize);
Total number of rows obtained
$count = News::getnewscount ();
Create a page splitter
$p = new PageView ($count [' 0 '] [' total '], $pageSize, $pageNo, $pageData);
Generate page numbers
$pageViewString = $p->echopageasdiv ();

http://www.bkjia.com/PHPjc/628968.html www.bkjia.com true http://www.bkjia.com/PHPjc/628968.html techarticle The so-called paging display, that is, the result set in the database is artificially divided into a section of the display, here need two initial parameters: how many records per page ($PageSize)? The current is ...

  • 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.