Home >Backend Development >PHP Tutorial >PHP universal paging class_PHP tutorial

PHP universal paging class_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:07:03907browse

class show_page {

    /**
     * 页面输出结果
     *
     * @var string
     */
 var $output;

    /**
     * 使用该类的文件,默认为 PHP_SELF
     *
     * @var string
     */
 var $file;

    /**
     * 页数传递变量,默认为 'p'
     *
     * @var string
     */
 var $pvar = "p";

    /**
     * 页面大小
     *
     * @var integer
     */
 var $psize;

    /**
     * 当前页面
     *
     * @var ingeger
     */
 var $curr;

    /**
     * 要传递的变量数组
     *
     * @var array
     */
 var $varstr;

    /**
     * 总页数
     *
     * @var integer
     */
    var $tpage;

    /**
     * 分页设置
     *
     * @access public
     * @param int $pagesize 页面大小
     * @param int $total    总记录数
     * @param int $current  当前页数,默认会自动读取
     * @return void
     */
    function set($pagesize=20,$total,$current=false) {
  global $HTTP_SERVER_VARS,$HTTP_GET_VARS;

  $this->tpage = ceil($total/$pagesize);
  if (!$current) {$current = $HTTP_GET_VARS[$this->pvar];}
  if ($current>$this->tpage) {$current = $this->tpage;}
  if ($current<1) {$current = 1;}

$this->curr  = $current;
  $this->psize = $pagesize;

  if (!$this->file) {$this->file = $HTTP_SERVER_VARS['PHP_SELF'];}

  if ($this->tpage > 1) {
           
   if ($current>10) {
    $this->output.='pvar.'='.($current-10).($this->varstr).' title="前十页"><<< ';
   }
            if ($current>1) {
    $this->output.='pvar.'='.($current-1).($this->varstr).' title="前一页"><< ';
   }

            $start = floor($current/10)*10;
            $end = $start+9;

            if ($start<1) {$start=1;}
if ($end>$this->tpage) {$end=$this->tpage;}

            for ($i=$start; $i<=$end; $i++) {
if ($current==$i) {
$this->output.=''.$i.' ';    //输出当前页数
                } else {
                    $this->output.='['.$i.'] ';    //输出页数
                }
            }

            if ($current<$this->tpage) {
    $this->output.='pvar.'='.($current+1).($this->varstr).' title="下一页">>> ';
   }
            if ($this->tpage>10 && ($this->tpage-$current)>=10 ) {
    $this->output.='pvar.'='.($current+10).($this->varstr).' title="下十页">>>>';
   }
  }
 }

    /**
     * 要传递的变量设置
     *
     * @access public
     * @param array $data   要传递的变量,用数组来表示,参见上面的例子
     * @return void
     */ 
 function setvar($data) {
  foreach ($data as $k=>$v) {
   $this->varstr.='&'.$k.'='.urlencode($v);
  }
 }

    /**
     * 分页结果输出
     *
     * @access public
     * @param bool $return 为真时返回一个字符串,否则直接输出,默认直接输出
     * @return string
     */
 function output($return = false) {
  if ($return) {
   return $this->output;
  } else {
   echo $this->output;
  }
 }

    /**
     * 生成Limit语句
     *
     * @access public
     * @return string
     */
    function limit() {
  return (($this->curr-1)*$this->psize).','.$this->psize;
 }

}


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445000.htmlTechArticleclass show_page { /** * 页面输出结果 * * @var string */ var $output; /** * 使用该类的文件,默认为 PHP_SELF * * @var string */ var $file; /** * 页数传递变量,默...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn