Share a simple paging code written in PHP

Source: Internet
Author: User
View code <HTML>
<Head>
<Title> paging </title>
</Head>
<Body>
<? PHP

// Connect to database
$ Conn = Mysql_connect ("Localhost", "root", "123456 ");
Mysql_select_db ("Test ", $ Conn );
Mysql_query ("Set names GBK ", $ Conn ); // Prevent garbled characters
$ Count = Mysql_query ("Select count (*) from myinfo ", $ Conn ); // Queries the total number of items, but returns a resource variable.
$ Count = Mysql_fetch_array ( $ Count );// Returns an array.
$ Count = $ Count [0]; // Here is the total number.

// Set the number of entries to be displayed on each page
$ Num= 3;

// Total number of pages
$ Page = 0;
If ( $ Count %$ Num ) // Returns true if not divisible
$ Page = Intval ( $ Count /3) + 1; // So add 1
Else
$ Page = Intval ($ Count /3 );

//Which of the following statements start the query?
$ First= 0;

If($ _ Get["Page"])//Get Page
$ First= ($ _ Get["Page"]-1) * 3;//This is the first page
Else 
$ First= 0;//If the returned value is null, It is 0. In fact, it can also be left blank, just for good looks. Because the default value already exists.

?>

<! -- The content is displayed, because the attributes of each record are: ID, name, age -->
<Table border = "1">
<Tr>
<TD> id </TD>
<TD> name </TD>
<TD> age </TD>
</Tr>

<? PHP
$ Rs = Mysql_query ("Select * From myinfo limit $ First , $ Num ", $ Conn ); // Start query. Limit indicates the total number of $ num entries to be queried on a page. the first row of the current page is $ first, and the resource is returned.
$ Myrow = Mysql_fetch_array ( $ Rs ); // Returns an associated array.
While ( $ Myrow ) // Loop output
{
Echo "<Tr> ";
Echo "<TD>$ Myrow [ID] </TD> <TD> $ Myrow [Name] </TD> <TD> $ Myrow [Age] </TD> ";
Echo "</Tr> ";
$ Myrow = Mysql_fetch_array ( $ Rs ); // After executing the current entry, You need to execute this statement again. Otherwise, you can only get the first record, which leads to an endless loop.
}
?>

</Table>

<? PHP
Echo"<Br> ";
Echo"Total ".$ Page. "The current page is :".$ _ Get["Page"]. "<br> ";//Displays the total number of pages. The preceding figure has been completed.

$ Last=$ _ Get["Page"]-1;//Previous Page
If($ Last= 0)
$ Last= 1;

$ Next=$ _ Get["Page"] + 1;//Next Page
If($ Next>$ Page)
$ Next=$ Page;

Echo "<A href = 'page. php? Page = ". $ Last . "'> [Previous Page] </a> "; // Previous Page button
$ Del = True ; // Used to determine the Display Mode
For ( $ I = 0; $ I < $ Page ; $ I ++) // Display highlights on the pages of [1] [2] [3 ]...
{
$ J = $ I + 1;

If ( $ J = 3) // We only display the first three items, which are simulated.
{
$ Del = False ;
Echo "<A href = 'page. php? Page = ". $ J . "'> [".$ J . "] </A> ...";
}
If ( $ Del ! = False )
Echo "<A href = 'page. php? Page = ". $ J . "'> [". $ J . "] </A> ";

}

Echo"<A href = 'page. php? Page = ".$ Next. "'> [Next Page] </a> ";//Next Page
?>
</Body>
</Html>

This is very simple. It is just used for sharing. Some functions have not been completed, so I wrote some. Others have read it.

 

 

 

 

 

 

 

 

 

 

 

 

 

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.