PHP code: ---------------------------------------------------------------------------------- htmlheadtitlePHP paging titleheadbody? To facilitate understanding and faster application to work, we take the NorthWind database MERS table of MSSQLServer as an example. $ PageSize4; PHP code displayed on each page :--------------------------------------------------------------------------------
PHP paging
// For ease of understanding and faster application to work, take the SQL Server's NorthWind database MERS table as an example.
$ PageSize = 4; // number of records displayed on each page
$ Hostname = "localhost"; // MSSQL Server
$ Dbuser = "sa"; // User name
$ Dbpasswd = ""; // password
// Connect to the database
$ Conn = mssql_connect ($ hostname, $ dbuser, $ dbpasswd) or die ("unable to connect to the database server! ");
// Select a database. for convenience, the pubs database of MSSQL Server is used as an example.
$ Db = mssql_select_db ("NorthWind", $ conn) or die ("unable to connect to the database! ");
// Take the MERS table as an example to construct a query string
$ SQL = "SELECT * FROM MERS ";
// Execute the query statement
$ Res = mssql_query ($ SQL) or die ("unable to execute SQL: $ SQL ");
// $ Page variable indicates the current page
If (! Isset ($ page) $ page = 1;
If ($ page = 0) $ page = 1;
// Obtain the number of records currently queried $ totalNum
$ TotalNum = mssql_num_rows ($ res );
If ($ totalNum <= 0)
{
Echo"
No record ";
Exit;
}
// Obtain the maximum number of pages maxPage
$ MaxPage = (int) ceil ($ totalNum/$ pageSize );
If (int) $ page> $ maxPage)
$ Page = $ maxPage;
?>
// Display the table headerFor ($ I = 0; $ I <mssql_num_fields ($ res); $ I ++){Echo"
". Mssql_field_name ($ res, $ I )." | ";}?>
// According to the offset ($ page-1) * $ pageSize, use the mssql_data_seek function to obtain the page to be displayed.If (mssql_data_seek ($ res, ($ page-1) * $ pageSize )){$ I = 0;// Display the current record set cyclicallyFor ($ I; $ I <$ pageSize; $ I ++ ){Echo"
";// Get the current record and fill it in the array $ row;$ Row = mssql_fetch_row ($ res );If ($ row){// Display all field values of the current record cyclicallyFor ($ j = 0; $ j <count ($ row); $ j ++){Echo"
". $ Row [$ j]." | ";}}Echo"
";}}?>
$ Style = "2 ";
Switch ($ style)
{
// Format: [Homepage] [Previous Page] [Next Page] [last page]
Case "1 ":
{
$ Out ="
";
$ Out. = "[total". $ maxPage. "page] [No.". $ page. "page]";
// Link between the home page and the previous page
If ($ totalNum> 1 & $ page> 1)
{
$ PrevPage = $ page-1;
$ Out. = "[Homepage]";
$ Out. = "[previous page]";
}
// Link to the next and last pages
If ($ page> = 1 & $ page <$ maxPage)
{
$ NextPage = $ page + 1;
$ Out. = "[Next Page]";
$ Out. = "[last page]";
}
$ Out. ="
";
Echo $ out;
}
Break;
// Format: 1 2 3 4 5
Case "2 ":
{
$ LinkNum = "4"; // The number of connections displayed on the page
$ Out ="
No ";
$ Start = ($ page-round ($ linkNum/2)> 0? ($ Page-round ($ linkNum/2): "1 ";
$ End = ($ page + round ($ linkNum/2) <$ maxPage? ($ Page + round ($ linkNum/2): $ maxPage;
If ($ page <> 1)
$ Out. = "1 <";
// For ($ t = 1; $ t <= $ maxPage; $ t ++)
For ($ t = $ start; $ t <= $ end; $ t ++)
{
$ Out. = ($ page = $ t )? "". $ T ."":" $ T ";
}
If ($ page <> $ maxPage)
$ Out. = ">>$ maxPage ";
$ Out. = "page
";
Echo $ out;
}
Break;
// Select drop-down box jump directly
Case "3 ":
{
$ Out ="
";
$ Out. = "no.";For ($ I = 1; $ I <= $ maxPage; $ I ++ ){$ Out. ="$ I";}$ Out. ="Page ";
$ Out. ="
";
Echo $ out;
}
Break;
Default:
Echo "";
Break;
}
?>