PHP簡易分頁代碼實現

來源:互聯網
上載者:User
PHP簡易分頁代碼實現

/**
* @todo 分頁
* @param $count 總條數
* @param $pagesize 每頁顯示條數
* @param $nowpagenum 當前頁碼
* @return String HTML
* @example
* $count = 45; //資料庫查詢到的資料總數
$pagesize = 10; //每頁展示的條數
$nowpagenum = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1 ; //擷取當前的頁碼,預設沒有的時候為1
$page = $this->PageNum($count, $pagesize ,$nowpagenum);
*
*/
public function PageNum($count , $pagesize ,$nowpagenum){
//計算頁碼
$pagenum = ceil(($count/$pagesize));
/***設定當前頁在第一頁當時無GET頁碼時預設為1***/
$nowpagenum = empty($nowpagenum) ? 1 : $nowpagenum;
/*****解析URL******/
$url = $_SERVER['REQUEST_URI'];
$urlArr = explode('?', $url);
if(count($urlArr)==1){
$newurl = $url.'?page=';
}else{
$params = array();
$newurl = '';
$newParams = array();
$paramsArr = explode('&', $urlArr[1]);
if(count($paramsArr)==1){
$getParams = explode('=', $paramsArr[0]);
$params[$getParams[0]] = $getParams[1];
$newurl = $urlArr[0].'?page=';
}else{
for($j=0;$j $getParams = explode('=', $paramsArr[$j]);
$params[$getParams[0]] = $getParams[1];
}
if(isset($params['page'])){
unset($params['page']);
foreach($params as $key=>$value){
$newParams[] = $key.'='.$value;
}
$newurl = $urlArr[0].'?'.implode('&', $newParams).'&page=';
}else{
foreach($params as $key=>$value){
$newParams[] = $key.'='.$value;
}
$newurl = $urlArr[0].'?'.implode('&', $newParams).'&page=';
}
}
}
/****拼裝頁碼顯示HTML代碼****/
$html='';
if(isset($count) && isset($pagesize)){
//one page
if($count <= $pagesize){
$html = '上一頁1下一頁';
//no record
}else if($count == 0){
$html = '暫未記錄!';
//大於一頁小於等於6頁
}else if($count > $pagesize && $pagenum <= 6){
if($nowpagenum==1){
$html .= '上一頁';
}else{
$html .= '上一頁';
}
for($i=1;$i<=$pagenum;$i++){
if($i==$nowpagenum){
$html .= ''.$i.'';
}else{
$html .= ''.$i.'';
}
}
if($nowpagenum==$pagenum){
$html .= '下一頁';
}else{
$html .= '下一頁';
}
}else{
if($nowpagenum==1){
$html .= '上一頁';
}else{
$html .= '上一頁';
}
if(($pagenum-$nowpagenum)>5){
if($nowpagenum<=3){
for($i=1;$i<=3;$i++){
if($i==$nowpagenum){
$html .= ''.$i.'';
}else{
$html .= ''.$i.'';
}
}
}else{
for($i=$nowpagenum-2;$i<=$nowpagenum;$i++){
if($i==$nowpagenum){
$html .= ''.$i.'';
}else{
$html .= ''.$i.'';
}
}
}
$html .= '...';
for($j=$pagenum-2;$j<=$pagenum;$j++){
if($j==$nowpagenum){
$html .= ''.$j.'';
}else{
$html .= ''.$j.'';
}
}
}else{
for($i=$pagenum-5;$i<=$pagenum;$i++){
if($i==$nowpagenum){
$html .= ''.$i.'';
}else{
$html .= ''.$i.'';
}
}
}
if($nowpagenum==$pagenum){
$html .= '下一頁';
}else{
$html .= '下一頁';
}
}
}
return $html;
}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.