Added several custom methods to extract specified fragments from the mysql result collection. The reason why the release is not called is related to the original verification of the program Thanks to the help of online experts. . . . The old system is killing people, the background cannot be touched
/* Pagination class
* @author xiaojiong & 290747680@qq.com
* @date 2011-08-17
*
* show (2) 1 ... 62 63 64 65 66 67 68 ... 150
* Pagination style
* #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; //Starting row number
public $list_rows; //Number of rows displayed on each page of the list
protected $total_pages; //Total number of pages
protected $total_rows; //Total number of rows
protected $now_page; //Number of current pages
protected $method = 'defalut'; //Handling Ajax paging Html paging (when static) Ordinary get method
protected $parameter = '';
protected $page_name; //The name of the paging parameter
protected $ajax_func_name;
public $plus = 3; //Paging offset
protected $url;
public function get_page_result()
{
$lastResult = array();
$skipCount = $this->get_skip_row_count();
if(mysql_num_rows($result)>0)
{
mysql_data_seek($result,$skipCount);
}
$pageSize = $this->$list_rows;
while($row = mysql_fetch_array($result))
{
$pageSize --;
$lastResult[] = $row;
if($pageSize == 0)
{
break ;
}
}
return $lastResult;
}
public function get_skip_row_count()
{
return $this->list_rows*($this->now_page-1);
}
/**
* Constructor
* @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'] : '';
/* Current page*/
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);
}
/**
* Get the current connection
* @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 '
' . $text . ' ' . "n";
break;
default:
return '< a href="' . $this->_get_url($page) . '">' . $text . '' . "n";
break;
}
}
/**
* Set current page link
*/
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;
}
/**
* Get the url of $page
* @param $page 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;
}
/**
* Get the first page
* @return string
*/
public function first_page($ name = 'First page')
{
if($this->now_page > 5)
{
return $this->_get_link('1', $name);
}
return '';
}
/**
* Last page
* @param $name
* @return string
*/
public function last_page($name = '最后一页')
{
if($this->now_page < $this->total_pages - 5)
{
return $this->_get_link($this->total_pages, $name);
}
return '';
}
/**
* Previous page
* @return string
*/
public function up_page($name = '上一页')
{
if($this->now_page != 1)
{
return $this->_get_link($this->now_page - 1, $name);
}
return '';
}
/**
* Next page
* @return string
*/
public function down_page($name = '下一页')
{
if($this->now_page < $this->total_pages)
{
return $this->_get_link($this->now_page + 1, $name);
}
return '';
}
/**
* Pagination style output
* @param $param
* @return string
*/
public function show($param = 1)
{
if($this->total_rows < 1)
{
return '';
}
$className = 'show_' . $param;
$classNames = get_class_methods($this);
if(in_array($className, $classNames))
{
return $this->$className();
}
return '';
}
protected function show_2()
{
if($this->total_pages != 1)
{
$return = '';
$return .= $this->up_page('<');
for($i = 1;$i<=$this->total_pages;$i++)
{
if($i == $this->now_page)
{
$return .= "
$i n";
}
else
{
if($this->now_page-$i>=4 && $i != 1)
{
$return .="
... n";
$i = $this->now_page-3;
}
else
{
if($i >= $this->now_page+5 && $i != $this->total_pages)
{
$return .="
... n";
$i = $this->total_pages;
}
$return .= $this->_get_link($i, $i) . "n";
}
}
}
$return .= $this->down_page('>');
return $return;
}
}
protected function show_1()
{
$plus = $this->plus;
if( $plus + $this->now_page > $this->total_pages)
{
$begin = $this->total_pages - $plus * 2;
}else{
$begin = $this->now_page - $plus;
}
$begin = ($begin >= 1) ? $begin : 1;
$return = '';
$return .= $this->first_page();
$return .= $this->up_page();
for ($i = $begin; $i <= $begin + $plus * 2;$i++)
{
if($i>$this->total_pages)
{
break;
}
if($i == $this->now_page)
{
$return .= "
$i n";
}
else
{
$return .= $this->_get_link($i, $i) . "n";
}
}
$return .= $this->down_page();
$return .= $this->last_page();
return $return;
}
protected function show_3()
{
$plus = $this->plus;
if( $plus + $this->now_page > $this->total_pages)
{
$begin = $this->total_pages - $plus * 2;
}else{
$begin = $this->now_page - $plus;
}
$begin = ($begin >= 1) ? $begin : 1;
$return = '总计 ' .$this->total_rows. ' 个记录分为 ' .$this->total_pages. ' 页, 当前第 ' . $this->now_page . ' 页 ';
$return .= ',每页 ';
$return .= '
';
$return .= $this->first_page()."n";
$return .= $this->up_page()."n";
$return .= $this->down_page()."n";
$return .= $this->last_page()."n";
$return .= '
'; for ($i = $begin;$i<=$begin+10;$i++) { if($i>$this->total_pages) { break; } if($i == $this->now_page) { $return .= ''.$i.' '; } else { $return .= '' .$i. ' '; } } $return .= ' ';
return $return;
}
}
?>