PHP头条
热点:

【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);
  	}
}
?>

www.phpzy.comtrue/php/25539.htmlTechArticle【PHP】php 分页类, ?php/* * 分页类 * * Author: DYmyw * Email: dymayongwei@163.com * Date: 2012/03/15 * Version: 1.0 */class Page{private $count;// 总条数private $pagesize;// 每页显示条数private $pagecount;// 总页数priv...

相关文章

    暂无相关文章

PHP之友评论

今天推荐