PHP分页查询

1、创建Page.php类,代码如下:

<?php
/**
 * 分页模板类
 */
class Page {
	private $cur_page;
	//当前页
	private $total;
	//总条数
	private $page_size = 10;
	//每页显示的条数
	private $total_page;
	//总页数
	private $first_page;
	//首页显示名称
	private $pre_page;
	//上一页的显示名称
	private $nex_page;
	//下一页的显示名称
	private $end_page;
	//尾页名称
	private $params;
	//分页后面的筛选参数
	private $num_size = 2;
	//当前页前后显示几个分页码
	private $base_url;
	//分页链接地址
	public function __construct(array $page_config=[]) {
		$this->cur_page = $page_config['cur_page'];
		$this->total = $page_config['total'];
		$this->page_size = $page_config['page_size'];
		$this->base_url = $page_config['base_url'];
		$this->pre_page = isset($page_config['pre_page']) ? $page_config['pre_page'] : "上一页";
		$this->nex_page = isset($page_config['next_page']) ? $page_config['next_page'] : "下一页";
		$this->end_page = isset($page_config['end_page']) ? $page_config['end_page'] : "尾页";
		$this->first_page = isset($page_config['first_page']) ? $page_config['first_page'] : "首页";
		$this->num_size = isset($page_config['num_size']) ? $page_config['num_size'] : 2;
		$this->params = isset($page_config['params']) ?$page_config['params'] : '';
		$this->total_page = ceil($this->total/$this->page_size);
	}
	/**
         * 获取首页的链接地址
         */
	public function get_first_page() {
		if ($this->cur_page > 1 && $this->cur_page != 1) {
			return $this->get_link($this->get_url(1),$this->first_page);
		}
		return '<span class="disabled">'.$this->first_page.'</span>';
	}
	/**
         * 获取上一页链接地址
         */
	public function get_prev_page() {
		if ($this->cur_page > 1 && $this->cur_page !=1) {
			return $this->get_link($this->get_url($this->cur_page-1),$this->pre_page);
		}
		return '<span class="disabled">'.$this->pre_page.'</span>';
	}
	/**
         * 获取下一页链接地址
         * @return string
         */
	public function get_next_page() {
		if ($this->cur_page < $this->total_page) {
			return $this->get_link($this->get_url($this->cur_page+1),$this->nex_page);
		}
		return '<span class="disabled">'.$this->nex_page.'</span>';
	}
	/**
         * 获取...符号
         * @return string
         */
	public function get_ext() {
		return '<span>...</span>';
	}
	/**
         * 获取尾页地址
         */
	public function get_end_page() {
		if ($this->cur_page < $this->total_page) {
			return $this->get_link($this->get_url($this->total_page),$this->end_page);
		}
		return '<span class="disabled">'.$this->end_page.'</span>';
	}
	/**
         * 中间的数字分页
         */
	public function now_bar() {
		if ($this->cur_page > $this->num_size) {
			$begin = $this->cur_page - $this->num_size;
			$end = $this->cur_page + $this->num_size;
		} else {
			$begin = 1;
			$end = 2*$this->num_size+1;			
		}
		//判断最后一页是否大于总页数
		if ($end > $this->total_page) {
			//重新计算开始页和结束页
			$begin = ($this->total_page - 2*$this->num_size > 0) ? $this->total_page - 2*$this->num_size : 1;
			//这里为什么用2*$this->num_size呢?因为当前页前后有2个$this->num_size的间距,所以这里是2*$this->num_size
			$end = $this->total_page;
		}
		$page_html='';
		for ($i=$begin;$i<=$end;$i++) {
			if ($i == $this->cur_page) {
				$page_html .= '<span class="disabled">'.$i.'</span>';
			} else {
				$page_html .= $this->get_link($this->get_url($i),$i);
			}
		}
		return $page_html;
	}
	/**
         * 输出分页码
         */
	public function show_page() {
		$show_page = '<div class="page_nav">';
		$ext = ($this->cur_page>$this->num_size) ? $this->get_ext() : '';
		$show_page.= $this->css();
		$show_page.= $this->show_total_row();
		//$show_page.= $this->show_total_page();
		$show_page.= $this->get_first_page();
		$show_page.= $this->get_prev_page();
		$show_page.= $ext;
		$show_page.= $this->now_bar();
		$show_page.= $ext;
		$show_page.= $this->get_next_page();
		$show_page.= $this->get_end_page();
		$show_page.='</div>';
		return $show_page;
	}
	/**
         * 获取分页地址 xxx.com/?page=5
         * @param $i
         */
	public function get_url($i) {
		return $this->base_url.'?page='.$i;
	}
	/**
         * 获取分页完整链接
         * @param $url
         */
	public function get_link($url,$text) {
		if ($this->params) $url.=$this->params;
		return "<a href='$url' target='_self' >$text</a>";
	}
	/**
         * 返回总条数
         * @return string
         */
	public function show_total_row() {
		return " 一共{$this->total}条数据 ";
	}
	/**
         * 返回总页数
         */
	public function show_total_page() {
		return "总页数:{$this->total_page} ";
	}
	/**
     * 分页样式
     */
    protected function css(){
		return '<style type="text/css">
        .page_nav { font-family: Simsun; line-height:normal;text-align: right;padding:20px 0 30px 0;overflow: hidden;zoom: 1;text-align:center}
        .page_nav a,.page_nav  span,.page_nav  input{display:inline-block;line-height:23px;padding:0 10px;border:1px solid #ccc;background-color:#fff; text-decoration:none;color:#666;margin-right:5px;zoom: 1;}
        .page_nav input{height: 23px;line-height: 23px;padding: 0;zoom: 1; font:12px/16px;font-family: Simsun;zoom: 1;_margin-bottom:-4px;}
        .page_nav a:hover,.page_nav span.pg_curr{color:#fff !important;background-color:#C00;border-color:#be0d11 #be0d11 #9a0307; text-decoration:none;}
        .disabled{ cursor: not-allowed;opacity: 0.6;}
		</style>';
	}
}

2、php显示数据列表和分页的页面

<?php 
include_once('../Page.php');
/**
 * 获取url中的参数
 * @param  $url 参数键名
 * @return 返回参数值
 */
function get_url_param($param){
	$current_url = $_SERVER["QUERY_STRING"];
	$arr = explode('&',$current_url);
	$value = '';
	foreach ($arr as $k=>$v) {
		$left_c = explode('=',$v);
		if ($left_c[0] == $param) {
			$value = $left_c[1];
			break;
		}
	}
	return $value;
}
?>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>操作日志</title>
    <link rel="stylesheet" href="../static/layui/css/layui.css">
    <link rel="stylesheet" href="../static/css/common.css">
</head>
<body>
    <div class="layui-card">
		<div class="layui-card-header">操作日志</div>
		<div class="layui-card-body">
		<table class="layui-table">
			<thead>
				<tr> 			
					<th>序号</th>
					<th>用户</th>
					<th>权限</th>
					<th>标记</th>
					<th>日期</th>
				</tr>
			</thead>
			<tbody>
			<?php
			//查询数据总条数
			$sql_count="SELECT COUNT(id) nums FROM `log`";
			$count_query = $mysql->query($sql_count);
			$row = $count_query->fetch_assoc();
			//总页数
			$total = $row['nums'];
			//每页显示多少条数据
			$page_size = 10;
			//当前页码
			$cur_page = empty(get_url_param('page')) ? 1 : get_url_param('page');
			$page = new Page([
				'cur_page'=>$cur_page,
				'total'=>$total,
				'page_size'=>$page_size,
				'base_url'=>$_SERVER['PHP_SELF'],
			]);
			
			//按照分页规律计算出数据起始条数
			$start = ($cur_page-1)*$page_size;
						
			$sql=sprintf("select * from `log`  order by `id` asc LIMIT %s,%s;",$start,$page_size);		
			
			$query = $mysql->query($sql);
						
			$list = array();
			while ($rows=mysqli_fetch_array($query,MYSQL_ASSOC)){
				$count=count($rows);
				for($i=0;$i<$count;$i++){  
					unset($rows[$i]);
				}
				array_push($list,$rows);
			}
			for ($i = count($list);$i > 0;$i--) {				
				echo '<tr>
					<td>'.$list[$i - 1]['id'].'</td>
					<td>'.$list[$i - 1]['name'].'</td>
					<td>'.$list[$i - 1]['role'].'</td>
					<td>'.$list[$i - 1]['mark'].'</td>
					<td>'.$list[$i - 1]['time'].'</td>
				</tr>';
			}
			?>
		  </tbody>
		</table>
		</div>
		<!--显示分页-->
		<?php echo $page->show_page();?>
	</div>
	<!--引入jquery框架-->
	<script src="../static/js/jquery.min.js" charset="utf-8"></script>
	<!--引入layui框架-->
	<script src="../static/layui/layui.js"></script>
	<script type="text/javascript">	
	layui.use(['form'], function(){
		var form = layui.form;
	});
	</script>
</body>
</html>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值