Home >php教程 >php手册 >求php的mssql翻页程序!

求php的mssql翻页程序!

WBOY
WBOYOriginal
2016-06-21 09:14:261661browse

程序|翻页

PHP代码:--------------------------------------------------------------------------------



PHP分页



//为了便于理解和更快地应用到工作中去,我们以MS SQL Server的NorthWind数据库Customers表为例。

$pageSize= 4; //每页显示的记录数
$hostname = "localhost"; //MSSQL Server
$dbuser = "sa"; //用户名
$dbpasswd = ""; //密码

//连接数据库
$conn = mssql_connect($hostname,$dbuser,$dbpasswd) or die("无法连接数据库服务器!");

//选择数据库,为了方便,这里以MSSQL Server的pubs数据库为例
$db = mssql_select_db("NorthWind",$conn) or die("无法连接数据库!");

//以Customers表为例,构建查询字符串
$sql = "SELECT * FROM Customers";

//执行查询语句
$res = mssql_query($sql) or die("无法执行SQL:$sql");

//$page变量标示当前显示的页
if(!isset($page)) $page=1;
if($page==0) $page=1;

//得到当前查询到的纪录数 $totalNum
$totalNum= mssql_num_rows($res);
if($totalNum{
echo "

没有纪录";
exit;
}

//得到最大页码数maxPage
$maxPage = (int)ceil($totalNum/$pageSize);

if((int)$page > $maxPage)
$page=$maxPage;

?>




//显示表格头
for($i = 0; $i {
echo "" ;
}
?>


//根据偏移量($page - 1)*$pageSize,运用mssql_data_seek函数得到要显示的页面
if( mssql_data_seek($res,($page-1)*$pageSize) )
{
$i=0;
//循环显示当前纪录集
for($i;$i echo "";

//得到当前纪录,填充到数组$row;
$row= mssql_fetch_row($res);
if($row)
{
//循环显示当前纪录的所有字段值
for($j = 0;$j {
echo "";
}
}
echo "";
}
}
?>
".mssql_field_name($res,$i)."
".$row[$j]."






$style = "2";
switch($style)
{
//格式: [首页] [上页] [下页] [末页]
case "1":
{
$out = "
";
$out .= "[共".$maxPage."页]  [第".$page."页]  ";

//首页和上页的链接
if( $totalNum>1 && $page>1)
{
$prevPage=$page-1;
$out .= " [首页]  ";
$out .= " [上页]  ";
}

//下页和末页的链接
if( $page>=1 && $page {
$nextPage= $page+1;
$out .= " [下页]  ";
$out .= " [末页]";
}
$out .= "
";
echo $out;
}
break;

//格式: 1 2 3 4 5
case "2":
{
$linkNum = "4";//页面上显示连接的个数显示
$out = "
第 ";
$start = ($page-round($linkNum/2))>0 ? ($page-round($linkNum/2)) : "1";
$end = ($page+round($linkNum/2)) if($page1)
$out .= "1   //for($t=1;$t for($t=$start;$t {
$out .= ($page==$t) ? "".$t."  " : "$t  ";
}
if($page$maxPage)
$out .= ">>  $maxPage";
$out .= " 页
";
echo $out;
}
break;

//select下拉框直接跳转
case "3":
{
$out = "
";
$out .= "第  页";
$out .= "
";
echo $out;
}
break;
default:
echo "";
break;
}

?>




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn