这篇文章主要为大家详细介绍了PHP分页函数的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!
准备数据:代码如下:
<?php
// Page分页函数
$page = $_GET["page"];
function Page($rows,$page_size){
global $page,$select_from,$select_limit,$pagenav;
$page_count = ceil($rows/$page_size);
if($page <= 1 || $page == '') $page = 1;
if($page >= $page_count) $page = $page_count;
$select_limit = $page_size;
$select_from = ($page - 1) * $page_size.',';
$pre_page = ($page == 1)? 1 : $page - 1;
$next_page= ($page == $page_count)? $page_count : $page + 1 ;
$pagenav .= "第 $page/$page_count 页 共 $rows 条记录 ";
$pagenav .= "<a href='?page=1'>首页</a> ";
$pagenav .= "<a href='?page=$pre_page'>前一页</a> ";
$pagenav .= "<a href='?page=$next_page'>后一页</a> ";
$pagenav .= "<a href='?page=$page_count'>末页</a>";
$pagenav.=" 跳到<select name='topage' size='1' onchange='window.location=\"?page=\"+this.value'>\n";
for($i=1;$i<=$page_count;$i++){
if($i==$page) $pagenav.="<option value='$i' selected>$i</option>\n";
else $pagenav.="<option value='$i'>$i</option>\n";
}
} // Page分页函数
// 使用示例
if (!$conn= mysql_connect("localhost", "root" ,"root")) die('数据库选择失败!');
if (!mysql_select_db("test", $conn)) die('数据库选择失败!');
mysql_query('set names GBK');
// 用Page函数计算出 $select_from 从哪条记录开始检索、$pagenav 输出分页导航
$rows = mysql_num_rows(mysql_query("select * from test"));
Page($rows,2);
$sql = "select * from test limit $select_from $select_limit";
$rst = mysql_query($sql);
while ($row = mysql_fetch_array($rst)){
echo "$row[id] - $row[sex] - $row[name] <hr />";
}
echo $pagenav;
?>
代码如下:
function getask(){
$sql = "select * from cms_ask where ansower <> ' ' ";
//这里要改成方法
$q_sq = mysql_query($sql);
$count = mysql_num_rows($q_sq);
$page_size = 8;
$page_current = isset($GLOBALS['page']) ? intval($GLOBALS['page']) : 1;
$page_count = ceil($count / $page_size);
$page_start = $page_current - 4;
$page_end = $page_current + 4;
if ($page_current < 5) {
$page_start = 1;
$page_end = 5;
}
if ($page_current > $page_count - 4) {
$page_start = $page_count - 8;
$page_end = $page_count;
}
if ($page_start < 1)
$page_start = 1;
if ($page_end > $page_count)
$page_end = $page_count;
$pagebar = "";
$sql = "select * from cms_ask where ansower <> ' ' order by id desc limit " . (($page_current - 1) * $page_size) . "," . $page_size;
$row=$this -> user -> getall("$sql");
foreach($row as $v){
echo '<dl><dt>问:'.$v["question"].'</dt><dd>答:'.$v["ansower"].date("Y-m-d H:i:s",$v["postTime"]).'</dd></dl>';
}
$pagebar .= "<div class=\"page\">";
$pagebar .= "<ol>";
if ($page_current != 1) {
$pagebar .= '<li><a href="javascript:get_comment(1)" class="sx">FIRST</a></li>';
}
for ($i = $page_start; $i <= $page_end; $i++) {
if ($i == $page_current) {
$pagebar .= "<li><span class=\"sort\">" . $i . "</span></li>";
} else {
$pagebar .= "<li><a href='javascript:get_comment(" . $i . ")'>" . $i . "</a></li>";
}
}
if ($page_current != $page_count) {
$pagebar .= "<li><a href='javascript:get_comment(" . $page_count . ")' class='sx'>END</a></li>";
}
$pagebar .= "</ol>";
$pagebar .= " </div>";
echo $pagebar;
}
本文来自:http://www.q1010.com/173/13272-0.html
注:关于PHP分页函数的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。