php分页类集锦

Original 2017-02-11 11:03:23 480
abstract:分页类一

分页类一

GetIndexBar () . $page->GetPageInfo (); print_r ( $result ); echo \"

\"; echo \"
\".$index.\"
\"; */ class Page { private $mTotalRowsNum = 0; // 总信息行数 private $mCurPageNumber = 1; // 当前所在页 private $mTotalPagesNum = 1; // 总页数 private $mQueryString; // 页面传递的数据(url?后的字符串) private $mPageRowsNum = 20; // 每页显示行数 private $mIndexBarLength = 11; // 索引条的页数 private $mIndexBar = ''; // 页码索引条 private $mPageInfo = ''; // 分页信息 // 页码索引条样式 private $mNextButton = \"8\"; private $mPreButton = \"7\"; private $mFirstButton = \"9\"; private $mLastButton = \":\"; private $mCssIndexBarCurPage = \"font-weight:bold;color:#FF0000\"; private $mCssIndexBarPage = ''; // 分页信息样式 private $mCssPageInfoNumFont = 'color:#FF0000'; private $mCssPageInfoFont = ''; // 构造方法 public function __construct(&$rSqlQuery, $userPageRowsNum = '') { if (! is_array ( $rSqlQuery )) { $this->SetDbPageBreak ( $rSqlQuery, $userPageRowsNum ); } else { $this->SetArrayPageBreak ( $rSqlQuery, $userPageRowsNum ); } } // 设置数据库型分页 private function SetDbPageBreak(&$rSqlQuery, $userPageRowsNum = '') { $this->SetDbTotalRowsNum ( $rSqlQuery ); $this->SetTotalPagesNum ( $userPageRowsNum ); if ($this->mTotalPagesNum > 1) { $this->SetCurPageNumber (); $this->SetSqlQuery ( $rSqlQuery ); $this->SetQueryString (); $this->SetIndexBar (); $this->SetPageInfo (); } } // 设置数组型分页 private function SetArrayPageBreak(&$rArray, $userPageRowsNum = '', $userTotalRowsNum = '') { $this->SetArrayTotalRowsNum ( $rArray, $userTotalRowsNum ); $this->SetTotalPagesNum ( $userPageRowsNum ); if ($this->mTotalPagesNum > 1) { $this->SetCurPageNumber (); $this->SetArray ( $rArray ); $this->SetQueryString (); $this->SetIndexBar (); $this->SetPageInfo (); } } // 数据库型计算总行数 private function SetDbTotalRowsNum($rSqlQuery) { $this->mTotalRowsNum = mysql_num_rows ( mysql_query ( $rSqlQuery ) ); } // 数组型计算总行数 private function SetArrayTotalRowsNum($array) { $this->mTotalRowsNum = count ( $array ); } // 计算总页数 private function SetTotalPagesNum($userPageRowsNum = '') { if ($userPageRowsNum) { $this->mPageRowsNum = $userPageRowsNum; } $this->mTotalPagesNum = ( int ) (floor ( ($this->mTotalRowsNum - 1) / $this->mPageRowsNum ) + 1); } // 计算当前页数 private function SetCurPageNumber() { if ($_GET ['page']) { $this->mCurPageNumber = $_GET ['page']; } } // 修正Sql截取语句 private function SetSqlQuery(&$rSqlQuery) { $start_number = ($this->mCurPageNumber - 1) * $this->mPageRowsNum; $rSqlQuery .= \" LIMIT \" . $start_number . \",\" . $this->mPageRowsNum; } // 修正截取后的Array private function SetArray(&$rArray) { $start_number = ($this->mCurPageNumber - 1) * $this->mPageRowsNum; $rArray = array_slice ( $rArray, $start_number, $this->mPageRowsNum ); } // 修正 $_GET 传递数据 private function SetQueryString() { $query_string = $_SERVER ['QUERY_STRING']; if ($query_string == '') { $this->mQueryString = \"?page=\"; } else { $this->mQueryString = preg_replace ( \"/&?page=\d+/\", '', $query_string ); $this->mQueryString = \"?\" . $this->mQueryString . \"&page=\"; } } // 设置页码索引条 private function GetPageIndex() { if ($this->mTotalPagesNum <= $this->mIndexBarLength) { $first_number = 1; $last_number = $this->mTotalPagesNum; } else { $offset = ( int ) floor ( $this->mIndexBarLength / 2 ); if (($this->mCurPageNumber - $offset) <= 1) { $first_number = 1; } elseif (($this->mCurPageNumber + $offset) > $this->mTotalPagesNum) { $first_number = $this->mTotalPagesNum - $this->mIndexBarLength + 1; } else { $first_number = $this->mCurPageNumber - $offset; } $last_number = $first_number + $this->mIndexBarLength - 1; } $last_number; for($i = $first_number; $i <= $last_number; $i ++) { if ($this->mCurPageNumber == $i) { $page_index .= \"\" . $i . \" \"; } else { $page_index .= \" \" . $i . \" \"; } } return $page_index; } // 设置页码索引条 private function SetIndexBar() { $this->mIndexBar = $this->GetNavFirstButton (); $this->mIndexBar .= $this->GetNavPreButton (); $this->mIndexBar .= $this->GetPageIndex (); $this->mIndexBar .= $this->GetNavNextButton (); $this->mIndexBar .= $this->GetNavLastButton (); } // 得到页码索引条 首页按钮 private function GetNavFirstButton() { return \"\" . $this->mFirstButton . \" \"; } // 得到页码索引条 上一页按钮 private function GetNavPreButton() { if ($this->mCurPageNumber > 1) { $pre_number = $this->mCurPageNumber - 1; } else { $pre_number = 1; } return \"\" . $this->mPreButton . \" \"; } // 得到页码索引条 下一页按钮 private function GetNavNextButton() { if ($this->mCurPageNumber < $this->mTotalPagesNum) { $next_number = $this->mCurPageNumber + 1; } else { $next_number = $this->mTotalPagesNum; } return \"\" . $this->mNextButton . \" \"; } // 得到页码索引条 末页按钮 private function GetNavLastButton() { return \"\" . $this->mLastButton . \" \"; } // 设置分页信息 private function SetPageInfo() { $this->mPageInfo = \"\"; $this->mPageInfo .= \"共 \" . $this->mTotalRowsNum . \" 条信息 | \"; $this->mPageInfo .= \"\" . $this->mPageRowsNum . \" 条/页 | \"; $this->mPageInfo .= \"共 \" . $this->mTotalPagesNum . \" 页 | \"; $this->mPageInfo .= \"第 \" . $this->mCurPageNumber . \" 页\"; $this->mPageInfo .= \"\"; } // 取出页码索引条 public function GetIndexBar() { return $this->mIndexBar; } // 取出分页信息 public function GetPageInfo() { return $this->mPageInfo; } //释放类 function __destruct() { } } ?>

分页类二

".($totle?($firstcount+1):0)."-".min($firstcount+$displaypg,$totle)." 条记录,共 $totle 条记录"; //如果只有一页则跳出函数: if($lastpg<=1) return false; $pagenav.=" 首页 "; if($prepg) $pagenav.=" 前页 "; else $pagenav.=" 前页 "; if($nextpg) $pagenav.=" 后页 "; else $pagenav.=" 后页 "; $pagenav.=" 尾页 "; //下拉跳转列表,循环列出所有页码: $pagenav.=" 到第  页,共 $lastpg 页"; } } //-------------------------具体演示举例------------------------------- /* //(前面程序略) include("pageft.php"); //包含“pageft.php”文件 //取得总信息数 $result=mysql_query("SELECT * from mytable"); $total=mysql_num_rows($result); //调用pageft(),每页显示10条信息(使用默认的20时,可以省略此参数),使用本页URL(默认,所以省略掉)。 pageft($total,10); //现在产生的全局变量就派上用场了: $result=mysql_query("SELECT * from mytable limit $firstcount,$displaypg "); while($row=mysql_fetch_array($result)){ //(列表内容略) } //输出分页导航条代码: echo $pagenav; //(后面程序略) */ ?>

分页类系列之三

total=$total; $this->page=$page; $this->num=$num; $this->pageNum=$this->getPageNum(); $this->offset=$this->getOffset(); } private function getPageNum(){ return ceil($this->total/$this->num); } private function getNextPage() { if($this->page==$this->pageNum) return false; else return $this->page+1; } private function getPrevPage() { if($this->page==1) return false; else return $this->page-1; } //数据库查询的偏移量 private function getOffset() { return ($this->page-1)*$this->num; } //当前页开始的记录数 private function getStartNum() { if($this->total==0) return 0; else return $this->offset+1; } //当前页结束的记录数 private function getEndNum() { return min($this->offset+$this->num,$this->total); } public function getPageInfo(){ $pageInfo=array( "row_total" => $this->total, "row_num" => $this->num, "page_num" => $this->getPageNum(), "current_page" => $this->page, "row_offset" => $this->getOffset(), "next_page" => $this->getNextPage(), "prev_page" => $this->getPrevPage(), "page_start" => $this->getStartNum(), "page_end" => $this->getEndNum() ); return $pageInfo; } } ?>

3种分页类使用起来效率都不错,也都很简单,这里推荐给大家。

更多关于php分页类集锦法请关注PHP中文网(m.sbmmt.com)其他文章!

Release Notes

Popular Entries