Summary of commonly used paging classes in PHP _ PHP Tutorial

Source: Internet
Author: User
Summary of commonly used paging classes in PHP. Paging is currently the best way to display a large number of results. With the help of the following code, developers can display a large amount of data on multiple pages. On the Internet, paging is currently the best way to display a large number of results. With the help of the following code, developers can display a large amount of data on multiple pages. On the Internet, pages are generally used for search results or browsing all information.

Php basic paging

The code is as follows:

// Database connection info
$ Conn = mysql_connect ('localhost', 'dbusername', 'dbpass') or trigger_error ("SQL", E_USER_ERROR );
$ Db = mysql_select_db ('dbname', $ conn) or trigger_error ("SQL", E_USER_ERROR );

// Find out how many rows are in the table
$ SQL = "SELECT COUNT (*) FROM numbers ";
$ Result = mysql_query ($ SQL, $ conn) or trigger_error ("SQL", E_USER_ERROR );
$ R = mysql_fetch_row ($ result );
$ Numrows = $ r [0];

// Number of rows to show per page
$ Rowsperpage = 10;
// Find out total pages
$ Totalpages = ceil ($ numrows/$ rowsperpage );

// Get the current page or set a default
If (isset ($ _ GET ['currentpage']) & is_numeric ($ _ GET ['currentpage']) {
// Cast var as int
$ Currentpage = (int) $ _ GET ['currentpage'];
} Else {
// Default page num
$ Currentpage = 1;
} // End if

// If current page is greater than total pages...
If ($ currentpage> $ totalpages ){
// Set current page to last page
$ Currentpage = $ totalpages;
} // End if
// If current page is less than first page...
If ($ currentpage <1 ){
// Set current page to first page
$ Currentpage = 1;
} // End if

// The offset of the list, based on current page
$ Offset = ($ currentpage-1) * $ rowsperpage;

// Get the info from the db
$ SQL = "SELECT id, number FROM numbers LIMIT $ offset, $ rowsperpage ";
$ Result = mysql_query ($ SQL, $ conn) or trigger_error ("SQL", E_USER_ERROR );

// While there are rows to be fetched...
While ($ list = mysql_fetch_assoc ($ result )){
// Echo data
Echo $ list ['id']. ":". $ list ['Number']."
";
} // End while

/****** Build the pagination links ******/
// Range of num links to show
$ Range = 3;

// If not on page 1, don't show back links
If ($ currentpage> 1 ){
// Show <link to go back to page 1
Echo "<";
// Get previous page num
$ Prevpage = $ currentpage-1;
// Show <link to go back to 1 page
Echo "<";
} // End if

// Loop to show links to range of pages around current page
For ($ x = ($ currentpage-$ range); $ x <($ currentpage + $ range) + 1); $ x ++ ){
// If it's a valid page number...
If ($ x> 0) & ($ x <= $ totalpages )){
// If we're on current page...
If ($ x = $ currentpage ){
// 'Highlight' it but don't make a link
Echo "[$ X] ";
// If not current page...
} Else {
// Make it a link
Echo "$ x ";
} // End else
} // End if
} // End

// If not on last page, show forward and last page links
If ($ currentpage! = $ Totalpages ){
// Get next page
$ Nextpage = $ currentpage + 1;
// Echo forward link for next page
Echo "> ";
// Echo forward link for lastpage
Echo "> ";
} // End if
/****** End build pagination links ******/
?>

First look at a common php paging class

The code is as follows:

/*
Place code to connect to your DB here.
*/
Include ('config. php'); // include your code to connect to DB.

$ Tbl_name = ""; // your table name
// How many adjacent pages shocould be shown on each side?
$ Adjacents = 3;

/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$ Query = "select count (*) as num FROM $ tbl_name ";
$ Total_pages = mysql_fetch_array (mysql_query ($ query ));
$ Total_pages = $ total_pages [num];

/* Setup vars for query .*/
$ Targetpage = "filename. php"; // your file name (the name of this file)
$ Limit = 2; // how many items to show per page
$ Page = $ _ GET ['Page'];
If ($ page)
$ Start = ($ page-1) * $ limit; // first item to display on this page
Else
$ Start = 0; // if no page var is given, set start to 0

/* Get data .*/
$ SQL = "SELECT column_name FROM $ tbl_name LIMIT $ start, $ limit ";
$ Result = mysql_query ($ SQL );

/* Setup page vars for display .*/
If ($ page = 0) $ page = 1; // if no page var is given, default to 1.
$ Prev = $ page-1; // previous page is page-1
$ Next = $ page + 1; // next page is page + 1
$ Lastpage = ceil ($ total_pages/$ limit); // lastpage is = total pages/items per page, rounded up.
$ Lpm1 = $ lastpage-1; // last page minus 1

/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$ Pagination = "";
If ($ lastpage> 1)
{
$ Pagination. ="

";
// Previous button
If ($ page> 1)
$ Pagination. = "��previous ";
Else
$ Pagination. = "��previous ";

// Pages
If ($ lastpage <7 + ($ adjacents * 2) // not enough pages to bother breaking it up
{
For ($ counter = 1; $ counter <= $ lastpage; $ counter ++)
{
If ($ counter = $ page)
$ Pagination. = "$ counter ";
Else
$ Pagination. = "$ counter ";
}
}
Elseif ($ lastpage> 5 + ($ adjacents * 2) // enough pages to hide some
{
// Close to beginning; only hide later pages
If ($ page <1 + ($ adjacents * 2 ))
{
For ($ counter = 1; $ counter <4 + ($ adjacents * 2); $ counter ++)
{
If ($ counter = $ page)
$ Pagination. = "$ counter ";
Else
$ Pagination. = "$ counter ";
}
$ Pagination. = "...";
$ Pagination. = "$ lpm1 ";
$ Pagination. = "$ lastpage ";
}
// In middle; hide some front and some back
Elseif ($ lastpage-($ adjacents * 2)> $ page & $ page> ($ adjacents * 2 ))
{
$ Pagination. = "1 ";
$ Pagination. = "2 ";
$ Pagination. = "...";
For ($ counter = $ page-$ adjacents; $ counter <= $ page + $ adjacents; $ counter ++)
{
If ($ counter = $ page)
$ Pagination. = "$ counter ";
Else
$ Pagination. = "$ counter ";
}
$ Pagination. = "...";
$ Pagination. = "$ lpm1 ";
$ Pagination. = "$ lastpage ";
}
// Close to end; only hide early pages
Else
{
$ Pagination. = "1 ";
$ Pagination. = "2 ";
$ Pagination. = "...";
For ($ counter = $ lastpage-(2 + ($ adjacents * 2); $ counter <= $ lastpage; $ counter ++)
{
If ($ counter = $ page)
$ Pagination. = "$ counter ";
Else
$ Pagination. = "$ counter ";
}
}
}

// Next button
If ($ page <$ counter-1)
$ Pagination. = "next ";
Else
$ Pagination. = "next ";
$ Pagination. ="

N ";
}
?>

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

// Your while loop here

}
?>



Instance

The code is as follows:

Class PageView {
/** Page number **/
Public $ pageNo = 1;
/** Page size **/
Public $ pageSize = 20;
/** Total number of pages **/
Public $ pageCount = 0;
/** Total number of records **/
Public $ totalNum = 0;
/** Offset, starting line of the current page **/
Public $ offSet = 0;
/** Data per page **/
Public $ pageData = array ();

/** Whether the previous page exists **/
Public $ hasPrePage = true;
/** Whether the next page exists **/
Public $ hasNextPage = true;

Public $ pageNoList = array ();

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

$ This-> totalNum = $ count; // total number of records
$ This-> pageSize = $ size; // size of each page
$ This-> pageNo = $ pageNo;
// Calculate the total number of pages
$ This-> pageCount = ceil ($ this-> totalNum/$ this-> pageSize );
$ This-> pageCount = ($ this-> pageCount <= 0 )? 1: $ this-> pageCount;
// Check pageNo
$ This-> pageNo = 0? 1: $ this-> pageNo;
$ This-> pageNo = $ this-> pageNo> $ this-> pageCount? $ This-> pageCount: $ this-> pageNo;

// Calculate the offset
$ This-> offset = ($ this-> pageNo-1) * $ this-> pageSize;
// Check whether the previous page is displayed
$ 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;
}
}

?>

Css code

The code is as follows:

Call methods on the php page

The code is as follows:

$ PageNo = $ _ GET ['pageno'];
If (empty ($ pageNo )){
$ PageNo = 1;
}
// Paging data
$ PageData = News: getNewsPage ($ pageNo, $ pageSize );
// Obtain the total number of rows
$ Count = News: getNewsCount ();
// Create a page splitter
$ P = new PageView ($ count ['0'] ['total'], $ pageSize, $ pageNo, $ pageData );
// Generate page number
$ PageViewString = $ p-> echoPageAsDiv ();


Bytes. With the help of the following code, developers can display a large amount of data on multiple pages. On the Internet ,...

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.