Find PHP's MSSQL page-flipping program!

Source: Internet
Author: User
Tags format end connect mssql mssql server sql php code query
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>
&LT;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

Format: 1 2 3 4 5
Case "2":
{
$linkNum = "4";//Display the number of connections on the page
$out = "<div align=center>";
$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. = "<a href= ' page=1 ' alt= ' home ' >1</a> <<";
for ($t =1; $t <= $maxPage; $t + +)
for ($t = $start; $t <= $end; $t + +)
{
$out. = ($page = = $t)? "<font color= ' Red ' ><b>". $t. " </b></font> ":" <a href= ' page= $t ' > $t </a> ';
}
if ($page <> $maxPage)
$out. = ">> <a href= '" page= $maxPage ' alt= ' last ' > $maxPage </a> ';
$out. = "Page </div>";
Echo $out;
}
Break

Select Drop-down Box Direct jump
Case "3":
{
$out = "<div align=center>";
$out. = "<select onchange=\" location= ' page= ' +this.options[this.selectedindex].value\ ">";
for ($i =1; $i <= $maxPage; $i + +) {
$out. = "<option value= ' $i '". ($i = = $page)? ' Selected ': '). " > $i </option> ";
}
$out. = "</select> page";
$out. = "</div>";
Echo $out;
}
Break
Default
echo "";
Break
}

?>
</body>


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.