PHP通用分页函数
发布时间:2015-11-06, 11:00:46 分类:PHP | 编辑 off 网址 | 辅助
图集1/1
正文 4376字数 131,706阅读

CSS
.pagenavi { margin:15px auto; padding:0 15px; height:30px; line-height:30px; }
.pagenavi.left { text-align:left; }
.pagenavi.center { text-align:center; }
.pagenavi a,.pagenavi span { display:inline-block; padding:0 8px; height:30px; line-height:30px; }
.pagenavi a { height:28px; line-height:28px; border:1px solid #d3d3d3; }
.pagenavi a.current { background:#d3d3d3; color:#333333; }
Run code
Cut to clipboard
通用分页函数
<?php
/**
* 通用分页函数
* @param Number $count 总记录数
* @param Number $perpage 每页记录数
* @param Number $page 当前页码
* @param String $url URL
* @param Array $prop 给page容器传入class, id等属性,为了样式通用
* @param Number $button_num 显示几个页码按钮
* @return String $html
* @author Baiyu 2013-11-23
*/
function page($count, $perpage, $page, $url, $prop = array('class' => 'pagenavi center'), $button_num = 5, $total_at = 'left') {
/* 开始page容器 */
$html = '<div';
foreach ($prop as $k => $v) {
$html .= ' ' . $k . '="' . $v . '"';
}
$html .= '>' . "\n";
//总数小于每页记录数不用分页
if ($count <= $perpage) {
return $html . "</div>\n";
}
/* 总记录条数 */
$total_record = '<span>共' . $count . '条记录</span>' . "\n";
$total_at == 'left' && $html .= $total_record;
/* 上一页 */
if ($page > 1) {
$html .= '<a href="' . str_replace('#page#', $page - 1, $url) . '">上一页</a>' . "\n";
}
//重要参数计算
$total_page = ceil($count / $perpage);
//总页数
$half_of_button_num = ceil($button_num / 2);
//离首尾页多远就需要添加首尾按钮和点点了
$need_dot = $total_page > $button_num + 2;
//需要出现首尾按钮和点点,点点也点2个按钮位故加2
$need_first_page = $need_dot && $page - 1 > $half_of_button_num;
//需要首页按钮
$need_last_page = $need_dot && $total_page - $page > $half_of_button_num;
//需要尾页按钮
$end_page = $need_last_page ? $page + $half_of_button_num - 1 : $total_page;
//页码按钮结束页
$start_page = $need_first_page ? ($need_last_page ? $page - $half_of_button_num + 1 : $end_page - $button_num) : 1;
//页码按钮开始页
/* 首页 */
if ($need_first_page) {
$html .= '<a href="' . str_replace('#page#', 1, $url) . '">1</a><span>...</span>' . "\n";
}
/* 页码按钮 */
$for_length = $need_dot ? ($button_num + 2 - intval($need_first_page) - intval($need_last_page)) : $total_page;
//无首尾页时可多放两个页码按钮且按钮总数等于总页码
for ($i = 0; $i < $for_length; $i++) {
$temp_page = $start_page + $i;
$html .= ('<a' . ($temp_page == $page ? ' class="current"' : '') . ' href="' . str_replace('#page#', $temp_page, $url) . '">' . $temp_page . '</a>' . "\n");
}
/* 尾页 */
if ($need_last_page) {
$html .= '<span>...</span><a href="' . str_replace('#page#', $total_page, $url) . '">' . $total_page . '</a>' . "\n";
}
/* 下一页 */
if ($page < $total_page) {
$html .= '<a href="' . str_replace('#page#', $page + 1, $url) . '">下一页</a>' . "\n";
}
$total_at != 'left' && $html .= $total_record;
/* 结束page容器 */
$html .= '</div>' . "\n";
return $html;
}
Run code
Cut to clipboard
使用教程,代码如下:
<?php
$sql="select * from php_orders where 1=1 ";
if(!empty($_REQUEST['ordersn'])){
$sql.=" and ordersn like '%".$_REQUEST['ordersn']."%' ";
}
if(!empty($_REQUEST['delivery'])){
$sql.=" and delivery = ".$_REQUEST['delivery'];
}
$sql.=" order by orderid desc";
//page start
$db->query($sql);
$page_total = $db->db_num_rows();
$page_perpage = 5;
$page_current = $_GET['page'] ? intval($_GET['page']) : 1; //当前第几页
if (strpos($_SERVER['REQUEST_URI'],'page=')){
$page_url = preg_replace('/page=(\d+)/', 'page=#page#', $_SERVER['REQUEST_URI']);
} elseif (strpos($_SERVER['REQUEST_URI'],'?')){
$page_url = $_SERVER['REQUEST_URI'] . '&page=#page#';
} else {
$page_url = $_SERVER['REQUEST_URI'] . '?page=#page#';
}
$page_html = page($page_total, $page_perpage, $page_current, $page_url, array('class' => 'pagenavi'));
//page end
$db->query($sql.' limit '.($page_current-1)*$page_perpage.','.$page_perpage);
while($row = $db->fetch_array()){
echo $row['orderid'];
}
echo $page_html;
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
暂无评论 »