小女子这厢有礼啦 把一维数组进行分页,有上一页下一页就好啦 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我有个现成的分页代码要不要?<?php/*********** cache.class.php* @ryan* 2013.08.31**********/class page{ public $first_row; public $list_rows; protected $total_pages; protected $total_rows; protected $now_page; protected $method='defalut'; protected $parameter=''; protected $page_name; protected $ajax_func_name; public $plus=3; protected $url; 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'] : 20; $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); } public function show($s=2){ if($this->total_rows < 1){ return ''; } $className='show_'.$s; $classNames=get_class_methods($this); if(!in_array($className, $classNames)){ $className='show_2'; } return $this->$className(); } 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='<div id="page">'; $return.=$this->first_page(); $return.=$this->pre_page(); for ($i=$begin;$i<=$begin+$plus*2;$i++){ if($i>$this->total_pages){ break; } if($i==$this->now_page){ $return .="<a class='now_page'>$i</a>"; }else{ $return .=$this->_get_link($i,$i); } } $return.=$this->next_page(); $return.=$this->last_page(); $return.='</div>'; return $return; } protected function show_2(){ if($this->total_pages>1){ $return='<div id="page">'; $return .=$this->pre_page('<'); for($i=1;$i<=$this->total_pages;$i++){ if($i==$this->now_page){ $return .="<a class='now_page'>$i</a>\n"; }else{ if($i<$this->now_page-$this->plus && $i>1){ $return .="<span class='pageMore'>...</span>"; $i=$this->now_page-1-$this->plus; }else{ if($i>$this->now_page+$this->plus && $i<$this->total_pages){ $return .="<span class='pageMore'>...</span>"; $i=$this->total_pages; } $return .=$this->_get_link($i, $i) . "\n"; } } } $return .=$this->next_page('>'); $return .='</div>'; return $return; } } protected function notice(){ $return='总计 ' .$this->total_rows. ' 个记录分为 ' .$this->total_pages. ' 页, 当前第 ' . $this->now_page . ' 页 '; $return .=',每页 '.$this->list_rows.' 个'; $url=$this->_get_url(); $return .="<select onchange='window.location.href=\"$url\"+this.value'>"; for ($i=1;$i<=$this->total_pages;$i++){ if($i==$this->now_page){ $return .='<option selected="true" value="'.$i.'">'.$i.'</option>'; } else{ $return .='<option value="' .$i. '">' .$i. '</option>'; } } $return .='</select>'; return $return; } 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; } protected function _get_url($page=''){ if($this->url===NULL){ $this->_set_url(); } return $this->url . $this->page_name . '=' . $page; } public function first_page($name='First'){ if($this->now_page > ($this->plus+1)){ return $this->_get_link('1', $name); } return ''; } public function last_page($name='Last'){ if($this->now_page < $this->total_pages-$this->plus){ return $this->_get_link($this->total_pages, $name); } return ''; } public function pre_page($name='Pre'){ if($this->now_page > 1){ return $this->_get_link($this->now_page - 1, $name); } return ''; } public function next_page($name='Next'){ if($this->now_page < $this->total_pages){ return $this->_get_link($this->now_page + 1, $name); } return ''; }}/*include 'page.class.php';//普通GET http://localhost/page/page_2?p=2$params = array('total_rows'=>1000, 'list_rows' =>20, ); //静态HTML类型 配合REWRITE重写$params = array('total_rows'=>1000, 'method' =>'html', 'parameter' =>'http://localhost/page/page_?', 'now_page' =>$_GET['page'], 'list_rows' =>20, ); //AJAX类型 无跳转刷新$params = array('total_rows'=>100, 'method' =>'ajax', 'ajax_func_name' =>'goToPage', 'now_page' =>1, #'parameter' =>"'jiong','username'", ); $page = new page($params);echo $page->show();*///普通GET http://localhost/page/page_2?p=2$params = array('total_rows'=>1000, 'list_rows' =>20, );$page = new page($params);echo $page->show(); ?><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}</style> http://www.w3school.com.cn/php/func_array_slice.asparray_slice() 函数在数组中根据条件取出一段值,并返回 $pagesize = 6;$data = array_chunk(str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), $pagesize);$page = @$_GET['page'];if($page < 1) $page = 1;if($page >= count($data)) $page = count($data);foreach($data[$page-1] as $v) echo " $v ";printf("<br><a href=?page=%d>上一页</a> <a href=?page=%d>下一页</a>", $page-1, $page+1); 如何利用PHP实现把EXCEL表中的数据导入数据库 通过浏览器和迅雷直接访问我的登录验证页会跳过登录,求解 smarty循环中的自加运算 \x7f-\xff疑惑 php向数据库填加记录的问题 求一个正则表达式,帮忙进来看看吧 关于current_date 文件属性值:0755/0777分别代表什么属性啊? 急 请问在php中如何求两个日期间相隔的天数 请问这个sql语句哪里错了? 帝国cms和php中 关于游戏网站 怎么将游戏列表的开服时间进行排序 PHPEXECL导入数据库乱码的问题
/**********
* cache.class.php
* @ryan
* 2013.08.31
**********/
class page{
public $first_row;
public $list_rows;
protected $total_pages;
protected $total_rows;
protected $now_page;
protected $method='defalut';
protected $parameter='';
protected $page_name;
protected $ajax_func_name;
public $plus=3;
protected $url;
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'] : 20;
$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);
}
public function show($s=2){
if($this->total_rows < 1){
return '';
}
$className='show_'.$s;
$classNames=get_class_methods($this);
if(!in_array($className, $classNames)){
$className='show_2';
}
return $this->$className();
}
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='<div id="page">';
$return.=$this->first_page();
$return.=$this->pre_page();
for ($i=$begin;$i<=$begin+$plus*2;$i++){
if($i>$this->total_pages){
break;
}
if($i==$this->now_page){
$return .="<a class='now_page'>$i</a>";
}else{
$return .=$this->_get_link($i,$i);
}
}
$return.=$this->next_page();
$return.=$this->last_page();
$return.='</div>';
return $return;
}
protected function show_2(){
if($this->total_pages>1){
$return='<div id="page">';
$return .=$this->pre_page('<');
for($i=1;$i<=$this->total_pages;$i++){
if($i==$this->now_page){
$return .="<a class='now_page'>$i</a>\n";
}else{
if($i<$this->now_page-$this->plus && $i>1){
$return .="<span class='pageMore'>...</span>";
$i=$this->now_page-1-$this->plus;
}else{
if($i>$this->now_page+$this->plus && $i<$this->total_pages){
$return .="<span class='pageMore'>...</span>";
$i=$this->total_pages;
}
$return .=$this->_get_link($i, $i) . "\n";
}
}
}
$return .=$this->next_page('>');
$return .='</div>';
return $return;
}
}
protected function notice(){
$return='总计 ' .$this->total_rows. ' 个记录分为 ' .$this->total_pages. ' 页, 当前第 ' . $this->now_page . ' 页 ';
$return .=',每页 '.$this->list_rows.' 个';
$url=$this->_get_url();
$return .="<select onchange='window.location.href=\"$url\"+this.value'>";
for ($i=1;$i<=$this->total_pages;$i++){
if($i==$this->now_page){
$return .='<option selected="true" value="'.$i.'">'.$i.'</option>';
}
else{
$return .='<option value="' .$i. '">' .$i. '</option>';
}
}
$return .='</select>';
return $return;
}
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;
}
protected function _get_url($page=''){
if($this->url===NULL){
$this->_set_url();
}
return $this->url . $this->page_name . '=' . $page;
}
public function first_page($name='First'){
if($this->now_page > ($this->plus+1)){
return $this->_get_link('1', $name);
}
return '';
}
public function last_page($name='Last'){
if($this->now_page < $this->total_pages-$this->plus){
return $this->_get_link($this->total_pages, $name);
}
return '';
}
public function pre_page($name='Pre'){
if($this->now_page > 1){
return $this->_get_link($this->now_page - 1, $name);
}
return '';
}
public function next_page($name='Next'){
if($this->now_page < $this->total_pages){
return $this->_get_link($this->now_page + 1, $name);
}
return '';
}
}
/*
include 'page.class.php';
//普通GET http://localhost/page/page_2?p=2
$params = array('total_rows'=>1000,
'list_rows' =>20,
);
//静态HTML类型 配合REWRITE重写
$params = array('total_rows'=>1000,
'method' =>'html',
'parameter' =>'http://localhost/page/page_?',
'now_page' =>$_GET['page'],
'list_rows' =>20,
);
//AJAX类型 无跳转刷新
$params = array('total_rows'=>100,
'method' =>'ajax',
'ajax_func_name' =>'goToPage',
'now_page' =>1,
#'parameter' =>"'jiong','username'",
);
$page = new page($params);
echo $page->show();
*/
//普通GET http://localhost/page/page_2?p=2
$params = array('total_rows'=>1000,
'list_rows' =>20,
);
$page = new page($params);
echo $page->show();
?>
<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}
</style>
函数在数组中根据条件取出一段值,并返回
$data = array_chunk(str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), $pagesize);$page = @$_GET['page'];
if($page < 1) $page = 1;
if($page >= count($data)) $page = count($data);
foreach($data[$page-1] as $v) echo " $v ";
printf("<br><a href=?page=%d>上一页</a> <a href=?page=%d>下一页</a>", $page-1, $page+1);