您好,欢迎访问一九零五行业门户网

PHP中常用的分页类总结_PHP教程

分页是目前在显示大量结果时所采用的最好的方式。有了下面这些代码的帮助,开发人员可以在多个页面中显示大量的数据。在互联网上,分​页是一般用于搜索结果或是浏览全部信息
php基本分页
 代码如下 复制代码
$totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage // set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql = select id, number from numbers limit $offset, $rowsperpage;
$result = mysql_query($sql, $conn) or trigger_error(sql, e_user_error);
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
// echo data
echo $list['id'] . : . $list['number'] .
;
} // end while
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show echo ;
// get previous page num
$prevpage = $currentpage - 1;
// show echo ;
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x // if it's a valid page number...
if (($x > 0) && ($x // if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo [$x] ;
// if not current page...
} else {
// make it a link
echo $x ;
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo > ;
// echo forward link for lastpage
echo >> ;
} // end if
/****** end build pagination links ******/
?>
先看一个常用的php分页类
 代码如下 复制代码
1)
 { 
  $pagination .=
;
  //previous button
  if ($page > 1)
   $pagination.= � previous;
  else
   $pagination.= � previous;
//pages 
  if ($lastpage   { 
   for ($counter = 1; $counter    {
    if ($counter == $page)
     $pagination.= $counter;
    else
     $pagination.= $counter;     
   }
  }
  elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
  {
   //close to beginning; only hide later pages
   if($page    {
    for ($counter = 1; $counter     {
     if ($counter == $page)
      $pagination.= $counter;
     else
      $pagination.= $counter;     
    }
    $pagination.= ...;
    $pagination.= $lpm1;
    $pagination.= $lastpage;  
   }
   //in middle; hide some front and some back
   elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
   {
    $pagination.= 1;
    $pagination.= 2;
    $pagination.= ...;
    for ($counter = $page - $adjacents; $counter     {
     if ($counter == $page)
      $pagination.= $counter;
     else
      $pagination.= $counter;     
    }
    $pagination.= ...;
    $pagination.= $lpm1;
    $pagination.= $lastpage;  
   }
   //close to end; only hide early pages
   else
   {
    $pagination.= 1;
    $pagination.= 2;
    $pagination.= ...;
    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter     {
     if ($counter == $page)
      $pagination.= $counter;
     else
      $pagination.= $counter;     
    }
   }
  }
//next button
  if ($page    $pagination.= next �;
  else
   $pagination.= next �;
  $pagination.=
n;  
 }
?> 
=$pagination?>
实例
 代码如下 复制代码
totalnum = $count;//总记录数
        $this->pagesize = $size;//每页大小
        $this->pageno = $pageno;
        //计算总页数
        $this->pagecount = ceil($this->totalnum/$this->pagesize);
        $this->pagecount = ($this->pagecountpagecount;
        //检查pageno
        $this->pageno = $this->pageno == 0 ? 1 : $this->pageno;
        $this->pageno = $this->pageno > $this->pagecount? $this->pagecount : $this->pageno;
//计算偏移
        $this->offset = ( $this->pageno - 1 ) * $this->pagesize;
        //计算是否有上一页下一页
        $this->hasprepage = $this->pageno == 1 ?false:true;
        $this->hasnextpage = $this->pageno >= $this->pagecount ?false:true;
$this->pagedata = $pagedata;
        $this->jsfunction = $jsfunction;
}
    /**
     * 分页算法
     * @return
     */
    private function generatepagelist(){
        $pagelist = array();
        if($this->pagecount             for($i=0;$ipagecount;$i++){
                array_push($pagelist,$i+1);
            }
        }else{
            if($this->pageno                 for($i=0;$i                    array_push($pagelist,$i+1);
                }
                array_push($pagelist,-1);
                array_push($pagelist,$this->pagecount);
            }else if($this->pageno > $this->pagecount - 4){
                array_push($pagelist,1);
array_push($pagelist,-1);
                for($i=5;$i>0;$i--){
                    array_push($pagelist,$this->pagecount - $i+1);
                }
            }else if($this->pageno > 4 && $this->pageno pagecount - 4){
                array_push($pagelist,1);
                array_push($pagelist,-1);
array_push($pagelist,$this->pageno -2);
                array_push($pagelist,$this->pageno -1);
                array_push($pagelist,$this->pageno);
                array_push($pagelist,$this->pageno + 1);
                array_push($pagelist,$this->pageno + 2);
array_push($pagelist,-1);
                array_push($pagelist,$this->pagecount);
}
        }
        return $pagelist;
    }
    /***
     * 创建分页控件
    * @param
    * @return string
    */
    public function echopageasdiv(){
        $pagelist = $this->generatepagelist();
$pagestring =
;
if(!empty($pagelist)){
            if($this->pagecount >1){
                if($this->hasprepage){
                    $pagestring = $pagestring .jsfunction . ( . ($this->pageno-1) . )>上一页;
                }
                foreach ($pagelist as $k=>$p){
                    if($this->pageno == $p){
                        $pagestring = $pagestring . . $this->pageno . ;
                        continue;
                    }
                    if($p == -1){
                        $pagestring = $pagestring ....;
                        continue;
                    }
                    $pagestring = $pagestring .jsfunction . ( . $p . )> . $p . ;
                }
if($this->hasnextpage){
                    $pagestring = $pagestring .jsfunction . ( . ($this->pageno+1) . )>下一页;
                }
}
        }
        $pagestring = $pagestring .(
);
        return $pagestring;
    }
}?>
css代码
 代码如下 复制代码
-->
在php页面中的调用方法
 代码如下 复制代码
$pageno = $_get['pageno'];
        if(empty($pageno)){
            $pageno = 1;
        }
        //分页数据
        $pagedata = news::getnewspage($pageno,$pagesize);
       //取得总行数
        $count = news::getnewscount();
        //创建分页器
        $p = new pageview($count['0']['total'],$pagesize,$pageno,$pagedata);
//生成页码
        $pageviewstring = $p->echopageasdiv();
http://www.bkjia.com/phpjc/444637.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/444637.htmltecharticle分页是目前在显示大量结果时所采用的最好的方式。有了下面这些代码的帮助,开发人员可以在多个页面中显示大量的数据。在互联网上,...
其它类似信息

推荐信息