PHP Paging Design

Source: Internet
Author: User
PHP Paging Design 1. Paging Design Page. php

Class Page {

Public $ pageSize = 6; // number of records displayed per page

Public $ res; // record set

Public $ rowCount; // The total number of records.

Public $ pageNow = 1; // current page

Public $ pageCount = 1; // total number of pages

Public $ navigate; // navigation

Public $ url; // address

Public $ firstRow; // The first record displayed on each page

Public $ rollPage; // The number of pages displayed on each page.

Public $ startNum; // [1]

Public $ endNum;


Public function setNav (){

$ Navigate = "";

// Start page of this navigation bar

If ($ this-> startNum> 1 ){

$ Jump = $ this-> startNum-$ this-> rollPage;

// $ Nav = $ this-> url. "/pageNow/$ jump ";

$ Navigate. = "url }? PageNow = $ jump '> <";

}

For ($ start = $ this-> startNum; $ start <= $ this-> endNum; $ start ++)

{

$ Navigate. = "url }? PageNow = $ start '> [{$ start}] ";

}

If ($ this-> endNum <$ this-> pageCount)

{

$ Jump = $ this-> startNum + $ this-> rollPage;

$ Navigate. = "url }? PageNow = $ jump '>>> ";

}

$ Navigate. = "". $ this-> pageNow. "page/total". $ this-> pageCount. "page ";

$ This-> navigate = $ navigate;

}


Public function setPage ($ url, $ pageNow, $ rowCount, $ pageSize = 6, $ rollPage = 4 ){


$ This-> url = $ url;

$ This-> pageNow = $ pageNow;

$ This-> rowCount = $ rowCount;

$ This-> pageSize = $ pageSize;

$ This-> rollPage = $ rollPage;

$ This-> pageCount = ceil ($ this-> rowCount/($ this-> pageSize + 0.0 ));

$ This-> firstRow = ($ this-> pageNow-1) * $ this-> pageSize;


$ This-> startNum = floor ($ this-> pageNow-1)/$ this-> rollPage) * $ this-> rollPage + 1;

$ This-> endNum =$ this-> startNum + $ this-> rollPage-1;

If ($ this-> endNum> $ this-> pageCount)

{

$ This-> endNum = $ this-> pageCount;

}

If ($ rowCount = 0 ){

$ This-> pageNow = 1;

$ This-> rollPage = 1;

$ This-> firstRow = 1;

$ This-> pageCount = 1;

}

$ This-> setNav (); // string storage navigation

}

}


?>



II. SqlHelper. class. php defines the paging display method

Public function excute_dql_page ($ sql1, $ sql2, & $ page ){

// Pagination of data table information

// Sql1 data, sql2 calculates the number of rows

$ Result = mysql_query ($ sql1, $ this-> conn) or die (mysql_errno ());

$ Arr = array ();

While ($ row = mysql_fetch_assoc ($ result )){

$ Arr [] = $ row;

}

Mysql_free_result ($ result );

$ Result = mysql_query ($ sql2, $ this-> conn) or die (mysql_errno ());

If ($ row = mysql_fetch_row ($ result )){

$ Page-> pageCount = ceil ($ row [0]/$ page-> pageSize );

$ Page-> rowCount = $ row [0];

}

Mysql_free_result ($ result );

// Implement navigation bar

$ Page-> res_array = $ arr; // Array Storage record set

$ FenyePage-> setNav ();

Return $ arr;

}

3. use pagination (userList. php)

Header ("content-type: text/html; charset = utf-8 ");

Require_once 'sqlhelper. class. php ';

Require_once 'userservice. class. php ';



Echo "user information table
";

Echo"


// Create fenyePage and set the attribute value



$ Page = new Page ();

$ Page-> url = "userList. php ";

$ Page-> pageNow = 1;

If (isset ($ _ GET ['pagenow']) {

$ Page-> pageNow =$ _ GET ['pagenow'];

}

// Create a Service to generate a record set (after pagination)

$ Page-> setPage ($ url, $ pageNow );

$ UserService = new UserService ();

$ UserService-> getUserListByPage ($ fenyePage );


Foreach ($ page-> res_array as $ row ){

$ Id = $ row ['id'];

Echo"

// Print_r ($ row ['id']);

}

Echo"

"; ";
Id Name Delete Modify
{$ Row ['id']} {$ Row ['name']} Delete Modify
";

Echo $ fenyePage-> navigate;

?>

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.