【PHP】php 分页类

<?php
/*
 * 分页类
 * 
 * Author: DYmyw
 * Email: dymayongwei@163.com
 * Date: 2012/03/15
 * Version: 1.0
 */
class Page{
	private $count;					// 总条数
	private $pagesize;				// 每页显示条数
	private $pagecount;				// 总页数
	private $pagetype;				// 显示的分页样式
	private $currentpagenum;		// 当前页码
	private $pagearg = "page";		// 分页参数名称
	private $argstr = "";			// $_GET 参数字符串
	public	$limit = "";			// SQL 语句 LIMIT 分页参数
	public	$pagearg_arr = array( "[ « ]", "[ ‹ ]", "[ › ]", "[ » ]" );
	
	// 构造函数
	public function __construct( $count, $pagesize = 5, $pagetype = 1 ){
		$this->count 	= intval( $count );
		$this->pagesize	= intval( $pagesize );
		$this->pagecount	= @ceil( $this->count / $this->pagesize );
		$this->get_current_page_num();
		$this->argstr	= $this->new_argstr();
		$this->limit	= " LIMIT ". ( $this->currentpagenum - 1 ) * $this->pagesize .", ". $this->pagesize;
		$this->pagesize = $pagesize;
	}
	
	// 获取当前页码
	private function get_current_page_num(){
		if ( isset( $_GET[$this->pagearg] ) ){
			if ( $_GET[$this->pagearg] <= 0 ){
				$this->currentpagenum = 1;
			}elseif ( $_GET[$this->pagearg] > $this->pagecount ){
				$this->currentpagenum = $this->pagecount;
			}else{
				$this->currentpagenum = $_GET[$this->pagearg];
			}
		}else {
			$this->currentpagenum = 1;
		}
	}
	
	// 整理传递参数
	private function new_argstr(){
		$get_arr = $_GET;
		unset( $get_arr[$this->pagearg] );
		
		if ( $get_arr ){
			foreach ( $get_arr as $key => $val ){
				// 防跨站攻击
				$val = htmlspecialchars( $val );
				$argstr .= $argstr ? "&$key=$val" : "?$key=$val";
			}
			$argstr .= "&$this->pagearg=";
		}else {
			$argstr = "?$this->pagearg=";
		}
		
		return $argstr;
	}
	
	// 显示分页
	public function page_info(){
		switch ( $this->pagetype ){
			case 1:
				return $this->page_css1();
				break;
			case 2:
				return $this->page_css2();
				break;
			default:
				return $this->page_css1();
				break;
		}
	}
	
	// 默认分页样式
	private function page_css1(){
		$page_css1_str = "";
  		$page_css1_str .= "共有记录:<span style='color:red;'>". $this->count ."</span> 条  ";
  		$page_css1_str .= "当前页次:<span style='color:red;'>". $this->currentpagenum ."</span> / <span style='color:red;'>". $this->pagecount ."</span> 页    ";
  		if ( $this->currentpagenum == 1 ){
  			$page_css1_str .= $this->pagearg_arr[0] ." ". $this->pagearg_arr[1] ." ";
  			$page_css1_str .= "<a href='". $this->argstr.( $this->currentpagenum + 1 ) ."' style='text-decoration:none;'>". $this->pagearg_arr[2] ."</a> ";
  			$page_css1_str .= "<a href='". $this->argstr.$this->pagecount ."' style='text-decoration:none;'>". $this->pagearg_arr[3] ."</a>    ";
  		}elseif ( $this->currentpagenum == $this->pagecount ){
  			$page_css1_str .= "<a href='". $this->argstr ."1' style='text-decoration:none;'>". $this->pagearg_arr[0] ."</a> ";
  			$page_css1_str .= "<a href='". $this->argstr.( $this->currentpagenum - 1 ) ."' style='text-decoration:none;'>". $this->pagearg_arr[1] ."</a> ";
  			$page_css1_str .= $this->pagearg_arr[2] ." ". $this->pagearg_arr[3] ."    ";
  		}else {
  			$page_css1_str .= "<a href='". $this->argstr ."1' style='text-decoration:none;'>". $this->pagearg_arr[0] ."</a> ";
  			$page_css1_str .= "<a href='". $this->argstr.( $this->currentpagenum - 1 ) ."' style='text-decoration:none;'>". $this->pagearg_arr[1] ."</a> ";
  			$page_css1_str .= "<a href='". $this->argstr.( $this->currentpagenum + 1 ) ."' style='text-decoration:none;'>". $this->pagearg_arr[2] ."</a> ";
  			$page_css1_str .= "<a href='". $this->argstr.$this->pagecount ."' style='text-decoration:none;'>". $this->pagearg_arr[3] ."</a>    ";
  		}
  		  		
  		$page_css1_str .= "转到:<select onChange='location.href=this.value;'>";
  		for ( $i=1; $i<=$this->pagecount; $i++ ){
  			if ( $this->currentpagenum == $i ){
  				$page_css1_str .= "<option value='". $this->argstr.$i ."' selected='selected'>第". $i ."页</option>";
  			}else {
  				$page_css1_str .= "<option value='". $this->argstr.$i ."'>第". $i ."页</option>";
  			}
  		}
  		$page_css1_str .= "</select>";
  		return $page_css1_str;
	}
	
	// 析构函数
  	public function __destruct(){
  		unset($this->count);
  		unset($this->pagesize);
  		unset($this->pagecount);  		
  		unset($this->pagetype);
  		unset($this->currentpagenum);
  		unset($this->pagearg);
  		unset($this->argstr);
  		unset($this->limit);
  	}
}
?>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
分页功能是通过将数据按照一定的规则进行分组,然后在页面上展示出来的一种常见的网页展示方式。在 PHP 中,我们可以使用 MySQL 数据库来实现分页功能。 以下是一个简单的 PHP 分页代码示例: ```php <?php // 连接数据库 $conn = mysqli_connect('localhost', 'username', 'password', 'database'); // 获取总记录数 $sql = "SELECT COUNT(*) FROM table_name"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_array($result); $total_records = $row[0]; // 每页显示数量 $per_page = 10; // 计算总页数 $total_pages = ceil($total_records / $per_page); // 获取当前页数 if (isset($_GET['page'])) { $current_page = $_GET['page']; } else { $current_page = 1; } // 计算起始位置 $start = ($current_page - 1) * $per_page; // 获取当前页数据 $sql = "SELECT * FROM table_name LIMIT $start, $per_page"; $result = mysqli_query($conn, $sql); // 输出数据 while ($row = mysqli_fetch_array($result)) { echo $row['column_name'] . '<br>'; } // 输出分页链接 for ($i = 1; $i <= $total_pages; $i++) { echo '<a href="?page=' . $i . '">' . $i . '</a> '; } ?> ``` 这段代码中,我们首先连接数据库,并查询出总记录数。然后,我们设置每页显示的数量和当前页数,并根据这些信息计算出总页数和起始位置。接着,我们使用 `LIMIT` 关键字从数据库中获取当前页的数据,并输出到页面上。最后,我们输出分页链接,使用户可以点击链接切换不同的页数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值