日期:2014-05-17  浏览次数:20616 次

PHP分页类
<?php
/**
 * 分页类
 * @author  xiaojiong & 290747680@qq.com
 * @date 2011-08-17
 * 
 * show(2)  1 ... 62 63 64 65 66 67 68 ... 150
 * 分页样式 
 * #page{font:12px/16px arial}
 * #page span{float:left;margin:0px 3px;}
 * #page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px 7px; text-decoration:none;color:#666}
 * #page a.now_page,#page a:hover{color:#fff;background:#05c}
*/

class Core_Lib_Page
{
	public     $first_row; 		  //起始行数

	public     $list_rows;  	  //列表每页显示行数
	
	protected  $total_pages;	  //总页数

	protected  $total_rows;		  //总行数
	
	protected  $now_page;         //当前页数
	
	protected  $method  = 'defalut'; //处理情况 Ajax分页 Html分页(静态化时) 普通get方式 
	
	protected  $parameter = '';
	
	protected  $page_name;        //分页参数的名称
	
	protected  $ajax_func_name;
	
	public 	   $plus = 3;         //分页偏移量
	
	protected  $url;
	
	
	/**
	 * 构造函数
	 * @param unknown_type $data
	 */
	public function __construct($data = array())
	{
		$this->total_rows = $data['total_rows'];

		$this->parameter 	    = !empty($data['parameter']) ? $data['parameter'] : '';
		$this->list_rows 		= !empty($data['list_rows']) && $data['list_rows'] <= 100 ? $data['list_rows'] : 15;
		$this->total_pages		= ceil($this->total_rows / $this->list_rows);
		$this->page_name  		= !empty($data['page_name']) ? $data['page_name'] : 'p';
		$this->ajax_func_name	= !empty($data['ajax_func_name']) ? $data['ajax_func_name'] : '';
		
		$this->method           = !empty($data['method']) ? $data['method'] : '';
		
		
		/* 当前页面 */
		if(!empty($data['now_page']))
		{
			$this->now_page = intval($data['now_page']);
		}else{
			$this->now_page   = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]):1;
		}
		$this->now_page   = $this->now_page <= 0 ? 1 : $this->now_page;
	
		
		if(!empty($this->total_pages) && $this->now_page > $this->total_pages)
		{
			$this->now_page = $this->total_pages;
		}
		$this->first_row = $this->list_rows * ($this->now_page - 1);
	}	
	
	/**
	 * 得到当前连接
	 * @param $page
	 * @param $text
	 * @return string
	 */
	protected function _get_link($page,$text)
	{
		switch ($this->method) {
			case 'ajax':
				$parameter = '';
				if($this->parameter)
				{
					$parameter = ','.$this->parameter;
				}
				return '<a onclick="' . $this->ajax_func_name . '(\'' . $page . '\''.$parameter.')" href="javascript:void(0)">' . $text . '</a>' . "\n";
			break;
			
			case 'html':
				$url = str_replace('?', $page,$this->parameter);
				return '<a href="' .$url . '">' . $text . '</a>' . "\n";
			break;
			
			default:
				return '<a href="' . $this->_get_url($page) . '">' . $text . '</a>' . "\n";
			break;
		}
	}
	
    
    /**
     * 设置当前页面链接
     */
    protected function _set_url()
    {
        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
        $parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params[$this->page_name]);
            $url   =  $parse['path'].'?'.http_build_query($params);
        }
        if(!empty($params))
        {
        	$url .= '&';
        }
        $this->url = $url;
    }
	
    /**
     * 得到$page的url
     * @param $page 页面
     * @return string
     */
    protected function _get_url($page)
    {
    	if($this->url === NULL)
    	{
    		$this->_set_url();	
    	}
  	//	$lable = strpos('&', $this->url) === FALSE ? '' : '&';
    	return $this->url . $this->page_name . '=' . $page;
    }
    
    
    /**
     * 得到第一页
     * @return string
     */
    public function first_page($name = '第一页')
    {
 		if($this->now_page > 5)
 		{
 			return $this->_get_link('1', $name);
 		}	
 		return '';
    }
    
    /**
     * 最后一页
     * @param $name
     * @return string
     */