Program | page Flip PHP code:--------------------------------------------------------------------------------
<title>php Paging </title>
<body>
?
To make it easier to understand and apply to work faster, we take MS SQL Server's Northwind database Customers table for example.
$pageSize = 4; Number of records displayed per page
$hostname = "localhost"; MSSQL Server
$dbuser = "sa"; User name
$DBPASSWD = ""; Password
Connecting to a database
$conn = Mssql_connect ($hostname, $dbuser, $dbpasswd) or die ("Unable to connect to the database server!") ");
Select database, for convenience, take the pubs database of MSSQL server for example
$db = mssql_select_db ("NorthWind", $conn) or die ("Unable to connect to the database!") ");
Take the Customers table as an example to build the query string
$sql = "SELECT * from Customers";
Executing query statements
$res = Mssql_query ($sql) or Die ("Unable to execute SQL: $sql");
$page variable to indicate the currently displayed page
if (!isset ($page)) $page = 1;
if ($page ==0) $page = 1;
The number of records to get the current query $totalNum
$totalNum = Mssql_num_rows ($res);
if ($totalNum <=0)
{
echo "<p align=center> no Record";
Exit
}
Get the maximum number of pages maxpage
$maxPage = (int) ceil ($totalNum/$pageSize);
if ((int) $page > $maxPage)
$page = $maxPage;
?>
<table align= "center" width= "90%" border= "1" cellspacing= "2" cellpadding= "2" >
<tr bgcolor= "#F7F2ff" >
?
Show Table Headers
for ($i = 0; $i < Mssql_num_fields ($res); $i + +)
{
echo "<td>". Mssql_field_name ($res, $i). " </td> ";
}
?>
</tr>
?
Using the Mssql_data_seek function to get the page to be displayed based on the offset ($page-1) * $pageSize
if (Mssql_data_seek ($res, ($page-1) * $pageSize))
{
$i = 0;
Loop to display the current record set
for ($i; $i < $pageSize; $i + +) {
echo "<tr>";
Gets the current record, fills the array $row;
$row = Mssql_fetch_row ($res);
if ($row)
{
Loops through all field values for the current record
for ($j = 0; $j < count ($row); $j + +)
{
echo "<td>". $row [$j]. " </td> ";
}
}
echo "</tr>";
}
}
?>
</table>
<br>
<HR size=1>
?
$style = "2";
Switch ($style)
{
Format: [Home] [prev] [Next page] [last]
Case "1":
{
$out = "<div align=center>";
$out. = "[Total]. $maxPage." Page] [$page. " Page] ";
Links to Home and previous pages
if ($totalNum >1 && $page >1)
{
$prevPage = $page-1;
$out. = "<a href= $PHP _self?page=1>[home]</a>";
$out. = "<a href= $PHP _self?page= $prevPage >[Upper page]</a>";
}
Links to the next and last pages
if ($page >=1 && $page < $maxPage)
{
$nextPage = $page +1;
$out. = "<a href= $PHP _self?page= $nextPage >[next page]</a>";
$out. = "<a href= $PHP _self?page= $maxPage >[last]</a>";
}
$out. = "</div>";
Echo $out;
}
Break
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.