PHP paging instance code

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
Class Page
{
Private $ pageSize; // The number of rows per page
Private $ absolutePage; // current page
Private $ pageCount; // total page number
Private $ totalNum; // total number of rows
Private $ prePage; // Previous Page
Private $ nextPage; // next page
Private $ sqlStr;
Private $ baseUrl; // url get transmission URL without the get Parameter
Function _ construct ($ pagesize, $ absolutepage, $ baseurl, $ sqlStr)
{
$ This-> absolutePage = $ absolutepage;
$ This-> pageSize = $ pagesize;
$ This-> baseUrl = $ baseurl;
$ This-> sqlStr = $ sqlStr;
}
// Display the subject content
Public function listinfo ()
{
$ Result = DBHelper: ExecuteCommand ($ this-> sqlStr );
// $ Result = mysql_query ($ this-> sqlStr, $ conn );
$ This-> totalNum = mysql_num_rows ($ result); // obtain the number of all rows in the result set.
$ This-> pageCount = (int) ($ this-> totalNum-1)/$ this-> pageSize) + 1; // calculates the total number of pages
If ($ this-> absolutePage = "" |! Is_numeric ($ this-> absolutePage )) // If the page is loaded for the first time, the value of absolutePage must be blank. In this case, we assume that the user is visiting the page for the first time. (Of course, if you want to show him the page 5th, you can also)
{
$ This-> absolutePage = 1; // show him the first page.
}
If ($ this-> absolutePage> $ this-> pageCount) // if the page number on the next page of the request exceeds the total page number, the last page is displayed.
{
$ This-> absolutePage = $ this-> pageCount; // display the last page
}
If ($ this-> totalNum> 1 & $ this-> absolutePage> 1) // if the previous page display condition is met, the variable value of the previous page is equal to the value of the current page (absolutePage) minus one.
{
$ This-> prePage = $ this-> absolutePage-1;
}
If ($ this-> absolutePage >=1 & $ this-> absolutePage <$ this-> pageCount) // assign a value of 1 to the variable on the next page when the current page is at least 1 and is not as large as the total number of pages.
{
$ This-> nextPage = $ this-> absolutePage + 1;
}
If (mysql_data_seek ($ result, ($ this-> absolutePage-1) * $ this-> pageSize )) // determines where the result set starts to retrieve data. mysql_data_seek points to the next row.
{
Include ("goodsTemplate. php ");
For ($ I = 0; $ I <$ this-> pageSize; $ I ++)
{
If ($ info = mysql_fetch_array ($ result )))
{
$ Name = $ info ['goodsname'];
$ Tupian = $ info ['goodsphopo'];
$ Id = $ info ['goodsid '];
$ Price = $ info ['goodsprice '];
$ Url = 'detail. php? Id = '. $ id;
$ Items = str_replace (array ('{name}', '{tupian}', '{url}', '{price}', '{id }'), array ($ name, $ tupian, $ url, $ price, $ id), $ goodsContent); // replace str_replace with two arrays. Replace the second with the first array, $ goodsContent parameter is the object to be replaced
$ Cishu ++;
Echo $ items;
If ($ cishu % 4 = 0)
{
?>
</Tr> <tr>
<? Php
}
}
}
}
}
// Paging Action Function
Public function toPage ()
{
Include ('topagetemplate. php ');
If ($ this-> totalNum> 1 & $ this-> absolutePage> 1)
{
$ This-> prePage = $ this-> absolutePage-1;
}
If ($ this-> absolutePage >=1 & $ this-> absolutePage <$ this-> pageCount)
{
$ This-> nextPage = $ this-> absolutePage + 1;
}
$ PreUrl = $ this-> baseUrl ."? AbsolutePage = $ this-> prePage ";
$ NextUrl = $ this-> baseUrl ."? AbsolutePage = $ this-> nextPage ";
$ Items = str_replace (array ('{totalNum}', '{absolutePage}', '{pageCount}', '{preUrl}', '{nextUrl }'), array ($ this-> totalNum, $ this-> absolutePage, $ this-> pageCount, $ preUrl, $ nextUrl), $ toPage );
Echo $ items;
}
}
?>

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.