Rumah > Artikel > pembangunan bahagian belakang > 求PHP+SQLServer的翻页成效
求PHP+SQLServer的翻页效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><br /><title>分页实例</title><br /><style><br />.page A{ padding:3px 5px; float:left; border:solid #CCCCCC 1px; text-decoration:none; font-size:10px; margin-left:1px; font-family:Verdana;color:#000000;}<br />.page A.currentpage{ color:#FF0000; border: solid red 1px;}<br />.page A:hover{ background-color:#CCCCCC;}<br /></style><br /></head><br /><br /><body><?php<br /><br />error_reporting(7);<br />//初始化翻页链接参数<br />$page=$_GET["page"]+0; //思考这里是用强制转换为整数还是用什么方法呢<br />$page= $page<=0 ? 1 : $page; //避免$_GET["page"]为负数和0的情况<br /><br />//分页尺寸<br />$page_size=10;<br /><br />$serverName = "(local)"; //数据库服务器地址 <br />$uid = "sa"; //数据库用户名 <br />$pwd = "ulptfstx"; //数据库密码 <br />$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"MSD"); <br />$conn = sqlsrv_connect( $serverName, $connectionInfo); <br /> if( $conn == false) {<br /> echo "连接失败!";<br /> die( print_r( sqlsrv_errors(), true));<br /> }<br /><br />//计算总行数<br />$total_records=sqlsrv_num_rows(sqlsrv_query("SELECT MB001 from INVMB"));<br /><br />//总页数<br />$total_page=ceil($total_records/$page_size); //进一法取整,避免最后一页不满$page_size, <br /><br />$page=$page>$total_page?$total_page:$page; //避免$_GET["page"]大于总页数<br /><br />//翻页链接开始<br />$page_link="";<br />if ($total_page>1)<br />{<br /> $page_link="<a>一共{$total_page}页</a>";<br /> $page_link.="<a>当前第{$page}页</a>";<br /> $page_link.="<a href=?page=1>首页</a>";<br /> <br /> if ($page>1){<br /> //页码大于1的时候,显示上一页翻页链接<br /> $pre_page=$page-1;<br /> $page_link.="<a href='?page=$pre_page'><<</a>";<br /> }<br /> //翻页列表<br /> for ($i=1;$i<=$total_page;$i++)<br /> {<br /> if ($i==$page)<br /> {<br /> //高亮当前页页码<br /> $page_link.="<a href='?page=$i' class='currentpage'>$i</a>";<br /> }<br /> else<br /> {<br /> $page_link.="<a href='?page=$i'>$i</a>";<br /> }<br /> }<br /> //页码小于总页数的时候显示下一页翻页链接<br /> $next_page=$page+1;<br /> if($next_page<$total_page)<br /> {<br /> $page_link.="<a href='?page={$next_page}'>>></a>";<br /> }<br /> $page_link.="<a href='?page=$total_page'>最后一页</a>";<br /> } <br /> //输出记录 <br /><br /> $offset=($page-1)*$page_size;<br /> $query=sqlsrv_query("select * from INVMB limit $offset,$page_size") or die(sqlsrv_error());<br /> echo "<ul>";<br /> while ($rs=sqlsrv_fetch_array($query))<br /> {<br /> echo "<li>{$rs["0"]}-{$rs["1"]}</li>";<br /> }<br /> echo "<ul>";<br /> echo "<div class='page'>$page_link</div>";<br /> <br /> ?><br /></body><br /></html>
Warning: sqlsrv_query() expects at least 2 parameters, 1 given in D:\Apache server\Apache\htdocs\bbs\demo.php on line 33<br><div class="clear"> </div>