• 技术文章 >php教程 >PHP源码

    二款php分页实例代码

    2016-06-08 17:25:22原创353


    !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<0))die("记录总数错误!");
    if((!is_int($pagesize))||($pagesize<0))die("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_pageno<$this->totalpage){
    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;$i<$begin+$this->pagebarnum;$i++){
    if($i<=$this->totalpage){
    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<=0)$pageno=1;
    return $this->set_format(''.$this->pre_bar."");
    }
    return $this->set_format(''.$this->pre_bar."");
    }
    /*“下一页”*/
    public function next_bar() {
    if($this->current_pageno<$this->totalpage-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<0))die("记录总数错误!");
    if((!is_int($pagesize))||($pagesize<0))die("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_pageno<$this->totalpage){
    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;$i<$begin+$this->pagebarnum;$i++)
    {
    if($i<=$this->totalpage){
    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<=0)$pageno=1;
    return $this->set_format(''.$this->pre_bar."");
    }
    return $this->set_format(''.$this->pre_bar."");
    }

    /**
    * 获取显示“下一分页条”的代码
    *
    * @return string
    */
    public function next_bar()
    {
    if($this->current_pageno<$this->totalpage-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;
    }

    }
    }
    ?>

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:nbsp this gt quot return
    上一篇:php读取txt文件中文乱码解决方法 下一篇:php文件上传之原理分析与上传类代码
    线上培训班

    相关文章推荐

    • 检验登陆类• php 数组查询 • 正则表达式三• 实现windows下,从php引入汉语命名的文件• 递归与迭代同时实现php快速排序

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网