Php paging implementation program code-PHP source code

Source: Internet
Author: User
This article summarizes several commonly used paging codes, such as the previous page and the next page. There is also a digital paging, such as 1, 2, 3... this article summarizes several commonly used paging codes, such as the previous page and the next page. There is also a digital paging, such as 1, 2, 3... and so on.

Script ec (2); script

Paging code

The Code is as follows:
// Page Current page, num page size per page
$ Page = isset ($ _ GET ['page'])? Intval ($ _ GET ['page']): 1;
$ Num = 5;
$ Conn = mysql_connect ('2017. 0.0.1 ', 'root ','');
If (! $ Conn ){
Die ('could not connect: '. mysql_error ());
}
Mysql_select_db ('shop ');
// Obtain the total number of records
$ Total = mysql_num_rows (mysql_query ("select * from user "));
// Calculate the page number
$ Pagenum = ceil ($ total/$ num );
// The maximum number of pages cannot be exceeded.
If ($ page> $ pagenum | $ page = 0 ){
Echo 'error: Can Not Found The page .';
Exit;
}
// The statement "Select * from table limit" extracts ten pieces of information from the table. 0 indicates the start point and 10 indicates the number of extracted items.
// $ Offset is the start value. For example, if the current page is the first page and there are 5 entries on each page, the start point is 0. Similarly, if the current page is the second page, the start point is 5.
$ Offset = ($ page-1) * $ num;
$ Result = mysql_query ("select * from user limit $ offset, $ num ");
While ($ it = mysql_fetch_array ($ result )){
Echo 'id: '. $ it ['id']. 'name:'. $ it ['name'].'
';
}
// Display the link page to switch the link. There is no link on the current page.
For ($ I = 1; $ I <= $ pagenum; $ I ++ ){
$ Show = ($ I! = $ Page )? "$ I ":"$ I";
Echo $ show ."";
}
Echo $ total. 'records, 5 records per page, total '. $ pagenum.' page ';
Mysql_free_result ($ result );
Mysql_close ($ conn );
?>

Instance 2

The Code is as follows:

// Function. php: The main function.
Function php_page ($ page_dbname, $ page_size, $ page)
{
If ($ page = "")
{$ Page = 1 ;};
If ($ ljjl = "")
{$ Ljjl = 0 ;};
If ($ page)
{
// $ Page_dbname = "tb_insert ";
// $ Page_size = 4; // four records are displayed on each page
$ Query = "select count (*) as total from". $ page_dbname; // read data from the database
$ Result = mysql_query ($ query );
$ Message_count = mysql_result ($ result, 0, "total"); // obtain the total number of records
$ Page_count = ceil ($ message_count/$ page_size); // obtain the total number of pages
$ Offset = ($ page-1) * $ page_size;
$ Query = "select * from". $ page_dbname. "order by id desc limit $ offset, $ page_size ";
$ Result = mysql_query ($ query );
// The content is displayed as a simple example. You can change the content as required.
While ($ myrow = @ mysql_fetch_array ($ result ))
{
Echo $ myrow [name]. "|". $ myrow [number]. "|". $ myrow [tel]. "|". $ myrow [address]."

";
}

}
Echo "page:". $ page ."/";
Echo $ page_count. "Page record :";
Echo $ message_count. "items "."
";

If ($ page! = 1)
{
Echo "Homepage ";
Echo "Previous Page ";
}
Else
{
Echo "Homepage ";
Echo "Previous Page ";
}

For ($ I = 1; $ I <= $ page_count; $ I ++)
{
If ($ page = $ I)
{Echo $ I ."";}
Else
{Echo "$ I ";}
}

If ($ page <$ page_count)
{
Echo "next page ";
Echo "last page ";
}
Else
{
Echo "next page ";
Echo "last page ";
}
}
?>

// Conn. php connects to the database

The Code is as follows:

$ Id = mysql_connect ("localhost", "root", "root") or dir ('Connection failed: '. mysql_error ());
// Mysql_connect ("IP", "User Name", "password ")

If (mysql_select_db ("db_database06", $ id ))
Echo "";
Else
Echo ('Connection failed: '. mysql_error ());
Mysql_query ("set names gb2312 ");
?>

// Main program, including the above two files, and then calls a php_page () to complete the paging. :)

The Code is as follows:

Require_once ("conn. php ");
Require_once ("function. php ");
Php_page ("tb_insert", 3, $ _ GET [page]); // "tb_insert" indicates the table name and 3 indicates the number of records to be displayed on each page, $ _ GET [page] is the page id, which can be used directly.
?>

On a text page, such as the previous page or next page, there is also a digital page, such as 1, 2, 3... and so on. This is relatively simple, and the former is a little more complicated. the last one is the combination of the two of them.

The Code is as follows:

--> 1 // obtain the current page number
If (isset ($ _ GET ['page']) {
$ Page = intval ($ _ GET ['page']);
}
Else {
$ Page = 1;
}
$ PageSize = 1; // number of records per page
// Obtain the total number
$ SQL = "select count (*) from blog ";
$ Result = mysql_query ($ SQL );
$ Row = mysql_fetch_row ($ result );
$ Amount = $ row [0];
/* Calculate the total number of pages
If ($ amount ){
If ($ amount <$ PageSize) {// if the total number is less than the number of records per page $ PageSize, there is only one page.
$ PageCount = 1;
}
If ($ amount % $ PageSize) {// The total number divided by the number of records on each page is obtained from
$ PageCount = & amp; nbsp; (int) ($ amount/$ PageSize) + 1; // If yes, the number of pages equals the total number of records on each page plus 1
}
Else {
$ PageCount = & amp; nbsp; $ amount/$ PageSize; // No, the result is the number of pages
}
}
Else {
$ PageCount = 0;
}*/
$ PageCount = ceil ($ amount/$ PageSize); // total number of pages = total number of pages divided by the number of pages. If a small tree exists, carry <span style = "color: #008000;">
If ($ Page> $ PageCount | $ page = 0) {// if the current Page number is greater than the total page number
Echo "this page cannot be found! ";
Exit ();
}

// Flip Link
$ PageOut = '';
If ($ page = 1) {// if the page number is only one page
$ PageOut. = 'page 1 | previous page ';
}
Else {
$ PageOut. = 'page 1 & lt;/a> | Previous Page | ';
}
If ($ page = $ PageCount | $ PageCount = 0) {// if the current page is equal to the total count
$ PageOut. = 'Next page | last page ';
}
Else {
$ PageOut. = 'Next page | last page ';
}
// Obtain data
If ($ amount ){
$ SQL = "select * from blog limit". ($ page-1) * ($ PageSize). ", $ PageSize ";
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ result) {// This code is just an example
$ Blogs [] = array ('bid' => $ row ['bid'], 'title' => $ row ['title']);
Foreach ($ blogs as $ blog ){
$ Title = $ blog ['title'];
}
$ Output = "delete ".
"Edit ".
"View ";
Include ("template/default/blog. tpl. php ");
Echo $ PageOut;
}
For ($ I = 1; $ I <= $ PageCount; $ I ++) {// numeric Paging
$ Pageshow = ($ I! = $ Page )? "$ I ":"$ I";
Echo $ PageShow;
}
Echo $ amount. 'record, entries on each page '. $ PageSize.', total '. $ PageCount.' page ';
}

Related Article

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.