分页页码显示算法
/** * 获取分页的HTML内容 * @param integer $page 当前页 * @param integer $pages 总页数 * @param string $url 跳转url地址 最后的页数以 '&page=x' 追加在url后面 * * @return string HTML内容; */ public static function getPageHtml($page, $pages, $url){ //最多显示多少个页码 $_pageNum = 5; //当前页面小于1 则为1 $page = $page<1?1:$page; //当前页大于总页数 则为总页数 $page = $page > $pages ? $pages : $page; //页数小当前页 则为当前页 $pages = $pages < $page ? $page : $pages; //计算开始页 $_start = $page - floor($_pageNum/2); $_start = $_start<1 ? 1 : $_start; //计算结束页 $_end = $page + floor($_pageNum/2); $_end = $_end>$pages? $pages : $_end; //当前显示的页码个数不够最大页码数,在进行左右调整 $_curPageNum = $_end-$_start+1; //左调整 if($_curPageNum<$_pageNum && $_start>1){ $_start = $_start - ($_pageNum-$_curPageNum); $_start = $_start<1 ? 1 : $_start; $_curPageNum = $_end-$_start+1; } //右边调整 if($_curPageNum<$_pageNum && $_end<$pages){ $_end = $_end + ($_pageNum-$_curPageNum); $_end = $_end>$pages? $pages : $_end; } $_pageHtml = '<ul class="pagination">'; /*if($_start == 1){ $_pageHtml .= '<li><a title="第一页">«</a></li>'; }else{ $_pageHtml .= '<li><a title="第一页" href="'.$url.'&page=1">«</a></li>'; }*/ if($page>1){ $_pageHtml .= '<li><a title="上一页" href="'.$url.'&page='.($page-1).'">«</a></li>'; } for ($i = $_start; $i <= $_end; $i++) { if($i == $page){ $_pageHtml .= '<li class="active"><a>'.$i.'</a></li>'; }else{ $_pageHtml .= '<li><a href="'.$url.'&page='.$i.'">'.$i.'</a></li>'; } } /*if($_end == $pages){ $_pageHtml .= '<li><a title="最后一页">»</a></li>'; }else{ $_pageHtml .= '<li><a title="最后一页" href="'.$url.'&page='.$pages.'">»</a></li>'; }*/ if($page<$_end){ $_pageHtml .= '<li><a title="下一页" href="'.$url.'&page='.($page+1).'">»</a></li>'; } $_pageHtml .= '</ul>'; echo $_pageHtml; }
其它:
<?php// 数据库连接常量define('DB_HOST', 'localhost');define('DB_USER', 'root');define('DB_PWD', '');define('DB_NAME', 'guest');// 连接数据库function conn(){$conn = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME);mysqli_query($conn, "set names utf8");return $conn;}//获得结果集function doresult($sql){$result=mysqli_query(conn(), $sql);return $result;}//结果集转为对象集合function dolists($result){return mysqli_fetch_array($result, MYSQL_ASSOC);}function totalnums($sql) {$result=mysqli_query(conn(), $sql);return $result->num_rows;}// 关闭数据库function closedb(){if (! mysqli_close()) {exit('关闭异常');}}?>
单实现了翻页功能,数据库请自行配置结构<?phpinclude 'mysqli.func.php';// 总记录数$sql = "SELECT dg_id FROM tb_user ";$totalnums = totalnums($sql);// 每页显示条数$fnum = 8;// 翻页数$pagenum = ceil($totalnums / $fnum);// 页数常量@$tmp = $_GET['page'];//防止恶意翻页if ($tmp > $pagenum)echo "<script>window.location.href='index.php'</script>";//计算分页起始值if ($tmp == "") {$num = 0;} else {$num = ($tmp - 1) * $fnum;}// 查询语句$sql = "SELECT dg_id,dg_username FROM tb_user ORDER BY dg_id DESC LIMIT " . $num . ",$fnum";$result = doresult($sql);// 遍历输出while (! ! $rows = dolists($result)) {echo $rows['dg_id'] . " " . $rows['dg_username'] . "<br>";}// 翻页链接for ($i = 0; $i < $pagenum; $i ++) {echo "<a href=index.php?page=" . ($i + 1) . ">" . ($i + 1) . "</a>";}?>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛