改进了那个基于ADODB的分页类,并附上了在Smarty中的实现
bigbenz8
![]()
|
1#
bigbenz8 发表于2007-04-16
改进了那个基于ADODB的分页类,并附上了在Smarty中的实现
--------------------------------------------------------改进后的分页类--------------------------------------------------
<?php class My_pager_i{ var $conn; var $tablename; var $curr_page;//当前页 var $page_size; function __construct($conn,$tablename,$page_size,$curr_page=1){ $this->conn=$conn; $this->tablename=$tablename; $this->curr_page=$curr_page; $this->page_size=$page_size; } //获得总页数 function getTotalPages(){ $str="select * from ".$this->tablename.""; $query_str=$this->conn->Execute($str); $total_records=$query_str->RecordCount(); $total_pages=ceil($total_records/$this->page_size); return $total_pages; }//end; //获得每一页的数据记录 function getPageRecords($curr_page,$order=""){ $this->curr_page=$curr_page; $data_pointer=($this->curr_page-1)*$this->page_size; $str="select * from ".$this->tablename." ".$order.""; $limit_sql=$this->conn->SelectLimit($str,$this->page_size,$data_pointer); $res=$limit_sql->GetAll(); return $res; }//end; //获得总记录数 function getRecordsNum(){ $str="select * from ".$this->tablename.""; $query=$this->conn->Execute($str); $num=$query->RecordCount(); return $num; }//end; //返回要显示的页码 function getPageNum($left,$right,$type,$curr_page=1){ $this->curr_page=$curr_page; $total=My_pager_i::getTotalPages(); $arr_links=array(); if($this->curr_page<1){ $this->curr_page=1; } if($this->curr_page>$total){ $this->curr_page=$total; } if($total==1){ $temp="<strong>$this->curr_page</strong>"; array_push($arr_links,$temp); } // if($total>1){ if($this->curr_page==1){ $temp="<strong>$this->curr_page</strong>"; array_push($arr_links,$temp); $right_page_limit=$this->curr_page+$right; if($right_page_limit>$total){ $right_page_limit=$total; } for($a=$this->curr_page+1;$a<=$total;$a++){ $temp2="<a href='".$_SERVER['PHP_SELF']."?page=$a&type=$type'>$a</a>"; array_push($arr_links,$temp2); } }//end; if($this->curr_page==$total){ $left_page_limit=$this->curr_page-$left; if($left_page_limit<1){ $left_page_limit=1; } for($b=$left_page_limit;$b<$this->curr_page;$b++){ $temp="<a href='".$_SERVER['PHP_SELF']."?page=$b&type=$type'>$b</a>"; array_push($arr_links,$temp); } $temp2="<strong>$this->curr_page</strong>"; array_push($arr_links,$temp2); }//end; if($this->curr_page>1 && $this->curr_page<$total){ $left_page_limit=$this->curr_page-$left; $right_page_limit=$this->curr_page+$right; if($left_page_limit<1){ $left_page_limit=1; } if($right_page_limit>$total){ $right_page_limit=$total; } for($c=$left_page_limit;$c<$this->curr_page;$c++){ $temp="<a href='".$_SERVER['PHP_SELF']."?page=$c&type=$type'>$c</a>"; array_push($arr_links,$temp); } $temp2="<strong>$this->curr_page</strong>"; array_push($arr_links,$temp2); for($d=$this->curr_page+1;$d<=$right_page_limit;$d++){ $temp3="<a href='".$_SERVER['PHP_SELF']."?page=$d&type=$type'>$d</a>"; array_push($arr_links,$temp3); } }//end; } return $arr_links; }//end function; }//end class; ?> --------------------------------------------------------------------以下是在Smarty中的实现----------------------------------------------------------- $my_pager_i=new My_pager_i($conn,"ganwu",20); $total_pages=$my_pager_i->getTotalPages(); $page_records=$my_pager_i->getPageRecords($my_page,"order by up_time desc"); $total_records=$my_pager_i->getRecordsNum(); $page_num=$my_pager_i->getPageNum(5,5,5,$my_page); $smarty->assign("totalPages",$total_pages); $smarty->assign("pageRecords",$page_records); $smarty->assign("totalRecords",$total_records); $smarty->assign("pageNum",$page_num); $smarty->assign("myType",$my_type); $smarty->display("listinfo.tpl"); ----------------------------------------------------------------------------------------------------------------------------------------------------------- <div id="holder"><div style="font-size:12px; color:#999999; padding-left:100px; padding-right:100px; margin-top:20px; word-break:break-all;"><ul style="list-style:square"><{section name=sec loop=$pageRecords}><li><a target="_blank" href="showArticle.php?type=<{$myType}>&id=<{$pageRecords[sec].id}>"> <{$pageRecords[sec].title}> [<{$pageRecords[sec].up_time}>]</a></li><{/section}></ul></div> <div align="right" id="tongji" style="font-size:12px; color:#999999; padding-left:100px; padding-right:100px;">共 <{$totalPages}> 页 <{$totalRecords}> 条记录 <a href="listinfo.php?type=<{$myType}>&page=1">|<<</a> <{section name=sec loop=$pageNum}><{$pageNum[sec]}> <{/section}><a href="listinfo.php?type=<{$myType}>&page=<{$totalPages}>">>>|</a></div> </div> |