PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

二款php分页实例代码

原创
2016-06-08 17:25:22 753浏览


!function_exists('cdstr') && exit('forbidden');
class wrzc_netpage {
private $page_name="p";
private $pagesize=10;//每页显示记录条数
private $total=0;//总的记录数
private $pagebarnum=10;//bar数。
private $totalpage=0;
private $linkhead="";//url地址头
private $current_pageno=1;//当前页
public function __construct($total,$pagesize=10) {
if((!is_int($total))||($total if((!is_int($pagesize))||($pagesize $this->set("total",$total);
$this->set("pagesize",$pagesize);
$this->set('totalpage',ceil($total/$pagesize));
}
public function set($var,$value){
if(in_array($var,get_object_vars($this)))
$this->$var=$value;
else {
throw new pb_page_exception("error in set():".$var." does not belong to pb_page!");
}
}
public function get_linkhead() {
$this->set_current_page();
if(empty($_server['query_string'])){
$this->linkhead=$_server['request_uri']."?".$this->page_name."=";
}else{
if(isset($_get[$this->page_name])){
$this->linkhead=str_replace($this->page_name.'='.$this->current_pageno,$this->page_name.'=',$_server['request_uri']);
} else {
$this->linkhead=$_server['request_uri'].'&'.$this->page_name.'=';
}
}
}
/*为指定的页面返回地址值*/
public function get_url($pageno=1){
if(empty($this->linkhead))$this->get_linkhead();
return str_replace($this->page_name.'=',$this->page_name.'='.$pageno,$this->linkhead);
}
/*当前页*/
public function set_current_page($current_pageno=0) {
if(empty($current_pageno)){
if(isset($_get[$this->page_name])){$this->current_pageno=intval($_get[$this->page_name]);}
}else{
$this->current_pageno=intval($current_pageno);
}
if ($this->current_pageno>$this->totalpage) header("location:./");//$this->current_pageno=1
}
public function set_format($str) {return $this->format_left.$str.$this->format_right;}
/* 获取显示"下一页"*/
public function next_page() {
if($this->current_pagenototalpage){
return ''.$this->next_page.'';
}
return '';
}
/*获取显示“上一页”*/
public function pre_page() {
if($this->current_pageno>1){return ''.$this->pre_page.'';}
return '';
}
/*获取显示“首页”*/
public function first_page() {return ''.$this->first_page."";}
/*获取显示“尾页”*/
public function last_page() {return ''.$this->last_page.'';}
public function nowbar() {
if ($this->totalpage > 1){
$begin=$this->current_pageno-ceil($this->pagebarnum/2);
$begin=($begin>=1)?$begin:1;
$return='';
for($i=$begin;$ipagebarnum;$i++){
if($itotalpage){
if($i!=$this->current_pageno){
$return.="".$i.'';
}else {
$return.='

'.$i.'
';
}
}else{
break;
}
}
unset($begin);
}
return $return;
}
/*“上一页”*/
public function pre_bar() {
if($this->current_pageno>ceil($this->pagebarnum/2)){
$pageno=$this->current_pageno-$this->pagebarnum;
if($pageno return $this->set_format(''.$this->pre_bar."");
}
return $this->set_format(''.$this->pre_bar."");
}
/*“下一页”*/
public function next_bar() {
if($this->current_pagenototalpage-ceil($this->pagebarnum/2)){
$pageno=$this->current_pageno+$this->pagebarnum;
return $this->set_format(''.$this->next_bar."");
}
return $this->set_format(''.$this->next_bar."");
}
/*跳转*/
public function select() {
$return='';
return $return;
}
public function pagebar($mode=1){
global $global;
$this->set_current_page();
$this->get_linkhead();
//return ("共有".$this->total."条记录。");
switch ($mode) {
case 1:
$this->pre_page='';
$this->next_page='';
return $this->pre_page().$this->nowbar().$this->next_page().'
'.$this->current_pageno.'页 / 共'.$this->totalpage.'
';
break;
case 2:
$this->pre_page='';
$this->next_page='';
return '
'.$this->current_pageno.'//m.sbmmt.com/m/'.$this->totalpage.'
'.$this->pre_page().$this->next_page();
//return '
'.$this->total.'//m.sbmmt.com/m/'.$this->current_pageno.'//m.sbmmt.com/m/'.$this->totalpage.'
'.$this->pre_page().$this->next_page();
break;
case 3:
$this->pre_page='';
$this->next_page='';
return '
第'.$this->current_pageno.'页/共'.$this->totalpage.'页
'.$this->pre_page().$this->next_page();
break;
}
}
}
?>

代码二

!function_exists('cdstr') && exit('forbidden');
class uobarpage {
private $page_name="p";//page标签,用来控制url页。比如说xxx.php?pb_page=2中的pb_page
private $pagesize=10;//每页显示记录条数
private $total=0;//总的记录数
private $pagebarnum=10;//控制记录条的个数。
private $totalpage=0;//总页数
private $linkhead="";//url地址头
private $current_pageno=1;//当前页
/**
* 显示符号设置
*/
private $next_page='>';//下一页
private $pre_page=' private $first_page='first';//首页
private $last_page='last';//尾页
private $pre_bar=' private $next_bar='>>';//下一分页条
private $format_left=' [';
private $format_right='] ';

public function __construct($total,$pagesize=10) {
if((!is_int($total))||($total if((!is_int($pagesize))||($pagesize $this->set("total",$total);
$this->set("pagesize",$pagesize);
$this->set('totalpage',ceil($total/$pagesize));
}

public function set($var,$value)
{
if(in_array($var,get_object_vars($this)))
$this->$var=$value;
else {
throw new pb_page_exception("error in set():".$var." does not belong to pb_page!");
}
}

/**
* get the default url获取指定的url地址
*
*/
/*
public function get_linkhead() {
$this->set_current_page();
$this->linkhead=$_server['php_self']."?".$this->page_name."=";
}
*/
public function get_linkhead() {
$this->set_current_page();
if(empty($_server['query_string'])){
$this->linkhead=$_server['request_uri']."?".$this->page_name."=";
}else{
if(isset($_get[$this->page_name])){
$this->linkhead=str_replace($this->page_name.'='.$this->current_pageno,$this->page_name.'=',$_server['request_uri']);
} else {
$this->linkhead=$_server['request_uri'].'&'.$this->page_name.'=';
}
}
}

/**
* 为指定的页面返回地址值
*/
public function get_url($pageno=1)
{
if(empty($this->linkhead))$this->get_linkhead();
return str_replace($this->page_name.'=',$this->page_name.'='.$pageno,$this->linkhead);
}

/**
* 设置当前页面
*
*/
public function set_current_page($current_pageno=0) {
if(empty($current_pageno)){
if(isset($_get[$this->page_name])){
$this->current_pageno=intval($_get[$this->page_name]);
}
}else{
$this->current_pageno=intval($current_pageno);
}
if ($this->current_pageno>$this->totalpage) header("location:./");//////////$this->current_pageno=1////////////
}

public function set_format($str) {
return $this->format_left.$str.$this->format_right;
}

/**
* 获取显示"下一页"的代码
*
* @return string
*/
public function next_page() {
if($this->current_pagenototalpage){
return ' '.$this->next_page.' ';
}
return '';
}

/**
* 获取显示“上一页”的代码
*
* @return string
*/
public function pre_page() {
if($this->current_pageno>1){
return ''.$this->pre_page.' ';
}
return '';
}

/**
* 获取显示“首页”的代码
*
* @return string
*/
public function first_page() {
return ''.$this->first_page."";
}

/**
* 获取显示“尾页”的代码
*
* @return string
*/
public function last_page() {
return ''.$this->last_page.'';
}

//gyl1
public function nowbar() {
$begin=$this->current_pageno-ceil($this->pagebarnum/2);
$begin=($begin>=1)?$begin:1;
$return='';
for($i=$begin;$ipagebarnum;$i++)
{
if($itotalpage){
if($i!=$this->current_pageno)
$return.=' '.''.$i.''.' ';
else
$return.=''.$i.'';
}else{
break;
}
}
unset($begin);
return $return;
}

/**
* 获取显示“上一分页条”的代码
*
* @return string
*/
public function pre_bar()
{
if($this->current_pageno>ceil($this->pagebarnum/2)){
$pageno=$this->current_pageno-$this->pagebarnum;
if($pageno return $this->set_format(''.$this->pre_bar."");
}
return $this->set_format(''.$this->pre_bar."");
}

/**
* 获取显示“下一分页条”的代码
*
* @return string
*/
public function next_bar()
{
if($this->current_pagenototalpage-ceil($this->pagebarnum/2)){
$pageno=$this->current_pageno+$this->pagebarnum;
return $this->set_format(''.$this->next_bar."");
}
return $this->set_format(''.$this->next_bar."");
}

/**
* 获取显示跳转按钮的代码
*
* @return string
*/
public function select()
{
$return='';
return $return;
}

/**
* 获取mysql教程 语句中limit需要的值
*
* @return string
*/
public function limit2(){
//return ("共有".$this->total."条记录。");
//return ('共有'.$this->total.'条记录。第'.$this->current_pageno)."页/共".$this->totalpage.'页';
return (''.$this->current_pageno.' / '.$this->totalpage.'');
}
public function pagebar($mode=1)
{ global $global;
$this->set_current_page();
$this->get_linkhead();
switch ($mode)
{
case '1':
$this->next_page='下一页';
$this->pre_page='上一页';
return $this->pre_page().$this->nowbar().$this->next_page();
//return $this->pre_page().$this->nowbar().$this->next_page().'第'.$this->select().'页';
break;
}

}
}
?>

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。