<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > |
<html xmlns= "http://www.w3.org/1999/xhtml" > |
<head> |
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> |
<title>商品分页</title> |
</head> |
<body> |
<?php |
//连接数据库 |
require "..\public\dbcn.php" ; |
$dbcn -> exec ( 'set names utf8' ); |
|
//定义页面大小 |
$pagesize =5; |
//求总记录数 |
//sql语句 |
$sql = "select count(*) from products" ; |
$rs = $dbcn ->query( $sql ); |
//在结果集$rs中取得一行数字索引数组行记录 |
$rows = $rs ->fetch(PDO::FETCH_NUM); |
$recordcount = $rows [0]; //总记录数 |
echo '记录总数:' . $recordcount . '<br />' ; |
//求总页数,ceil()函数是向上取整 |
$pagecount = ceil ( $recordcount / $pagesize ); |
echo '总页码:' . $pagecount . '<br />' ; |
//获得当前传递的页码 |
$pageno =isset( $_GET [ 'pageno' ])? $_GET [ 'pageno' ]:1; |
echo '当前页码:' . $pageno . '<br />' ; |
if ( $pageno <1){ |
$pageno =1; |
} |
if ( $pageno > $pagecount ){ |
$pageno = $pagecount ; |
} |
//求当前页的起始位置 |
$startno =( $pageno -1)* $pagesize ; |
//获取当前页面内容 |
$sql = "select * from products limit $startno,$pagesize" ; |
$rs = $dbcn ->query( $sql ); |
?> |
</p> |
<table width= "800" border= "1" > |
<tr> |
<th width= "99" align= "center" >编号</th> |
<th width= "99" align= "center" >商品名称</th> |
<th width= "99" align= "center" >规格</th> |
<th width= "99" align= "center" >价格</th> |
<th width= "114" align= "center" >库存量</th> |
<th width= "120" align= "center" >图片</th> |
<th width= "124" align= "center" >网站</th> |
</tr> |
<?php |
//循环取出一条记录匹配成关联数组 |
while ( $rows = $rs ->fetch(PDO::FETCH_ASSOC)){ |
echo '<tr>' ; |
echo '<td>' . $rows [ 'proID' ]. '</td>' ; |
echo '<td>' . $rows [ 'proname' ]. '</td>' ; |
echo '<td>' . $rows [ 'proguide' ]. '</td>' ; |
echo '<td>' . $rows [ 'proprice' ]. '</td>' ; |
echo '<td>' . $rows [ 'proamount' ]. '</td>' ; |
echo '<td>' . $rows [ 'proimages' ]. '</td>' ; |
echo '<td>' . $rows [ 'proweb' ]. '</td>' ; |
echo '<tr>' ; |
} |
?> |
</table> |
</form> |
<table> |
<tr> |
<td> |
<a href= "? pageno=1" >首页</a> |
<a href= "? pageno=<?php echo $pageno-1;?>" >上一页</a> |
<a href= "? pageno=<?php echo $pageno+1;?>" >下一页</a> |
<a href= "? pageno=<?php echo $pagecount;?>" >尾页</a> |
|
</td> |
<td> |
<?php |
for ( $i =1; $i <= $pagecount ; $i ++){ |
echo '<a href="fenye.php ? pageno=' . $i . '</a> ' ; |
} |
?> |
</td> |
<tr> |
</table> |
</body> |
</html> |