php分页封装
封装显示分页码函数
/*生成分页页码(最多显示5页) @param int $num 文章总数 @param int $curr 当前显示的页码 @param int $cnt 每页显示的条数 */ function getPage($num,$curr,$cnt){ $max = ceil($num/$cnt); //最大页码数 $left = max(1,$curr-2); //最左页码数 $right = min($left+4,$max); //最右页码数 $left = max(1,$right-4); $page = array(); for($i=$left;$i<=$right;$i++){ $_GET['page'] = $i; $page[$i] = http_build_query($_GET); } return $page; }
sql部分
//判断地址栏是否有cat_id if(isset($_GET['cat_id'])){ $cat_id = $_GET['cat_id']; $where = " and art.cat_id =$cat_id "; }else { $where = ""; } //分页代码 $sql = "select count(*) from art where 1" .$where ; //获取总的文章数 $num = mGetOne($sql); $curr =isset($_GET['page']) ? $_GET['page'] : 1; //当前页码数 $cnt = 2; //每页显示条数 $page = getPage($num,$curr,$cnt); $artsql = "select art_id,title,content,pubtime,comm,catname from art left join cat on art.cat_id=cat.cat_id where 1".$where . ' order by art_id desc limit ' .($curr-1)*$cnt .','.$cnt; $arts = mGetAll($artsql); include('./view/front/index.html');
模板部分
<div id="pagebar"> Pages: <a href="index.php?page=1"><</a> <?php foreach($page as $k=>$v) {?> <a href="index.php?<?php echo $v;?>"><?php echo $k;?></a> <?php } ?> <a href="index.php?page=<?php echo ceil($num/$cnt)?>">></a> </div>
上一篇:php获取来访用户真实IP
下一篇:原生php图片上传