所谓分页显示,也就是将数据库中的结果集人为的分成一段一段的来显示,这里需要两个初始的参数: 每页多少条记录($PageSize)?
当前是第几页($CurrentPageID)?
现在只要再给我一个结果集,我就可以显示某段特定的结果出来.
至于其他的参数,比如:上一页($PReviousPageID)、下一页($NextPageID)、总页数($numPages)等等,都可以根据前边这几个东西得到.
以MySQL数据库为例,如果要从表内截取某段内容,sql语句可以用:select * from table limit offset, rows.看看下面一组sql语句,尝试一下发现其中的规率.
前10条记录:select * from table limit 0,10
第11至20条记录:select * from table limit 10,10
第21至30条记录:select * from table limit 20,10
……
这一组sql语句其实就是当$PageSize=10的时候取表内每一页数据的sql语句,我们可以总结出这样一个模板:
select * from table limit ($CurrentPageID – 1) * $PageSize, $PageSize
拿这个模板代入对应的值和上边那一组sql语句对照一下看看是不是那么回事.搞定了最重要的如何获取数据的问题以后,剩下的就仅仅是传递参数,构造合适的sql语句然后使用php从数据库内获取数据并显示了.以下我将用具体代码加以说明.
3、简单代码
请详细阅读以下代码,自己调试运行一次,最好把它修改一次,加上自己的功能,比如搜索等等.
实例代码如下:
- <?php
-
- $link = mysql_connect(“localhost”, “mysql_user”, “mysql_passWord”)
- or die(“Could not connect: ” . mysql_error());
-
- if( isset($_GET['page']) ){
- $page = intval( $_GET['page'] );
- }
- else{
- $page = 1;
- }
-
- $PageSize = 10;
-
- $sql = “select count(*) as amount from table”;
- $result = mysql_query($sql);
- $row = mysql_fetch_row($result);
- $amount = $row['amount'];
-
- if( $amount ){
- if( $amount < $page_size ){ $page_count = 1; }
- if( $amount % $page_size ){
- $page_count = (int)($amount / $page_size) + 1;
- }else{
- $page_count = $amount / $page_size;
- }
- }
- else{
- $page_count = 0;
- }
-
- $page_string = ”;
- if( $page == 1 ){
- $page_string .= ‘第一页|上一页|’;
- }
- else{
- $page_string .= ‘<a href=?page=1>第一页</a>|<a href=?page=’.($page-1).’>上一页</a>|’;
- }
- if( ($page == $page_count) || ($page_count == 0) ){
- $page_string .= ‘下一页|尾页’;
- }
- else{
- $page_string .= ‘<a href=?page=’.($page+1).’>下一页</a>|<a href=?page=’.$page_count.’>尾页</a>’;
- }
-
- if( $amount ){
- $sql = “select * from table order by id desc limit “. ($page-1)*$page_size .”, $page_size”;
- $result = mysql_query($sql);
- while ( $row = mysql_fetch_row($result) ){
- $rowset[] = $row;
- }
- }else{
- $rowset = array();
- }
-
- ?>
PHP分页器制作,自动生成分面页码,JS调用函数
实例代码如下:
- <?php
- class PageView{
-
- public $pageNo = 1;
-
- public $pageSize = 20;
-
- public $pageCount = 0;
-
- public $totalNum = 0;
-
- public $offSet = 0;
-
- public $pageData = array();
-
- public $hasPrePage = true;
-
- public $hasNextPage = true;
- public $pageNoList = array();
- public $jsFunction ='jsFunction';
-
-
-
-
-
-
- public function __construct($count=0, $size=20,$pageNo=1,$pageData =array(),$jsFunction='jsFunction'){
- $this->totalNum = $count;
- $this->pageSize = $size;
- $this->pageNo = $pageNo;
-
- $this->pageCount = ceil($this->totalNum/$this->pageSize);
- $this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;
-
- $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;
- }
-
-
-
-
- private function generatePageList(){
- $pageList = array();
- if($this->pageCount <= 9){
- for($i=0;$i<$this->pageCount;$i++){
- array_push($pageList,$i+1);
- }
- }else{
- if($this->pageNo <= 4){
- for($i=0;$i<5;$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 <= $this->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;
- }
-
-
-
-
-
- public function echoPageAsDiv(){
- $pageList = $this->generatePageList();
- $pageString ="<div class='pagination'><div class='page-bottom'>";
- if(!emptyempty($pageList)){
- if($this->pageCount >1){
- if($this->hasPrePage){
- $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")">上一页</a>";
- }
- foreach ($pageList as $k=>$p){
- if($this->pageNo == $p){
- $pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>";
- continue;
- }
- if($p == -1){
- $pageString = $pageString ."<span class='page-break'>...</span>";
- continue;
- }
- $pageString = $pageString ."<a href="javascript:" .$this->jsFunction . "(" . $p . ")">" . $p . "</a>";
- }
- if($this->hasNextPage){
- $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")">下一页</a>";
- }
- }
- }
- $pageString = $pageString .("</div></div>");
- return $pageString;
- }
- }
- ?>
- <style type="text/css">
- <!--
- .pagination {font-family: Tahoma;overflow: hidden; padding-top: 12px; text-align: center;}
- .pagination-tab { margin-bottom: 20px;}
- .pagination a, .pagination .page-cur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, .pagination .page-skip {
- display: inline-block;font-family: Tahoma,SimSun,Arial; height: 22px;line-height:22px; margin: 0; min-width: 16px;padding: 0 5px; text-align: center; vertical-align: top; white-space: nowrap;}
- .pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break {
- border: 1px solid #ed3d83; color:#e9357d; font-weight:bold;}
- .pagination a:hover { border: 1px solid #ed3d83;text-decoration: none; background-color:#f95f9d; color:#fff;}
- .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width: 36px; background-image: url(../static/img/page.gif);}
- .pagination .page-prev { background-position: -0px -38px; padding-left: 16px;}
- .pagination .page-prev_g { background-position:0px -59px; padding-left: 16px; color:#cbcbcb; font-weight:normal;}
- .pagination .page-next { background-position: 0px 0px; padding-right: 16px; font-weight:normal;}
- .pagination .page-next_g { background-position: -0px -19px; padding-right: 16px; color:#cbcbcb;}
- .pagination .page-cur {background-color: #f95f9d; border: 1px solid #ed3d83;color: #fff;font-weight: bold;}
- .pagination .page-break {border: medium none; background:none transparent; color:#333;}
- -->
- </style>
PHP调用实例代码如下:
- $pageNo = $_GET['pageNo'];
- if(emptyempty($pageNo)){
- }
-
- $pageData = News::getNewsPage($pageNo,$pageSize);
-
- $count = News::getNewsCount();
-
- $p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData);
-
- $pageViewString = $p->echoPageAsDiv();
(责任编辑:admin) |