PHP分頁類,生成分頁html字符串


 1 <?php  2 namespace Common\Common;  3 
 4 /**  5  * 該Page類主要有兩個方法:showPageString(), showPageStringAsAJAX()  6  *  7  * showPageString():主要用於生成普通的超鏈接的html分頁代碼,頁面也會隨之刷新。  8  *  9  * showPageStringAsAJAX():主要用於利用ajax發送異步請求,生成帶有js函數和參數的html分頁代碼。  10  *  11  * @author Fly 2016/09/25  12  *  13  */
 14 class Page{  15     
 16     /**  17  * 通用的分頁html字符串  18  *  19  * @param int $currentPage 當前頁面  20  * @param int $pageSize 頁面條數  21  * @param int $totalCount 總條數  22  *  23  * @return string $output 返回的分頁Html字符串  24      */
 25     public function showPageString($currentPage, $pageSize, $totalCount){  26         $redirectTo = "/Admin/Score/loadOrderInfo";//分頁每次跳轉的地址
 27         $pageSize = $pageSize == 0 ? 3 : $pageSize;  28         
 29         $totalPages = floor(($totalCount + $pageSize - 1) / $pageSize); //總頁數
 30         $output = '<nav><ul class="pagination">';//輸出字符串
 31         if ($totalPages > 1)  32  {  33             //if (currentPage != 1)
 34             {//處理首頁連接
 35                 $output .= "<li><a  href=".$redirectTo."?pageIndex=1&pageSize=".$pageSize."'>首頁</a></li>";  36                 
 37  }  38             if ($currentPage > 1)  39             {//處理上一頁的連接
 40                 $output .= "<li><a  href='".$redirectTo."?pageIndex=".($currentPage -1)."&pageSize=".$pageSize."'>上一頁</a></li>";  41  }  42             else
 43  {  44                 // output.Append("<span class='pageLink'>上一頁</span>");
 45  }  46         
 47             $output .= " ";  48             $currint = 5;  49             for ($i = 0; $i <= 10; $i++)  50             {//一共最多顯示10個頁碼,前面5個,后面5個
 51                 if (($currentPage + $i - $currint) >= 1 && ($currentPage + $i - $currint) <= $totalPages)  52  {  53                     if ($currint == $i)  54                     {//當前頁處理  55  //output.Append(string.Format("[{0}]", currentPage));
 56                         $output .= "<li class='active'><a  href='javascript:void(0);'>$currentPage</a></li>";  57  }  58                     else
 59                     {//一般頁處理
 60                         $output .= "<li><a  href='".$redirectTo."?pageIndex=".($currentPage + $i - $currint)."&pageSize=".$pageSize."'>".($currentPage + $i - $currint)."</a></li>";  61  }  62  }  63                 $output .= " ";  64  }  65             if ($currentPage < $totalPages)  66             {//處理下一頁的鏈接
 67                 $output .= "<li><a  href='".$redirectTo."?pageIndex=".($currentPage + 1)."&pageSize=".$pageSize."'>下一頁</a></li>";  68  }  69             else
 70  {  71                 //output.Append("<span class='pageLink'>下一頁</span>");
 72  }  73             $output .=" ";  74             if ($currentPage != $totalPages)  75  {  76                 $output .= "<li><a  href='".$redirectTo."?pageIndex=".$totalPages."&pageSize=".$pageSize."'>末頁</a></li>";  77  }  78             $output .= "</ul></nav>";  79  }  80         //$output .= "第".$currentPage."頁 / 共".$totalPages."頁";//這個統計加不加都行
 81         
 82         return $output;  83  }  84     
 85     /**  86  * 利用ajax的異步分頁  87  *  88  * @param string $jumpFun 對應js的function名字  89  * @param int $currentPage 當前頁面  90  * @param int $pageSize 頁面條數  91  * @param int $totalCount 總條數  92  *  93  * @return string $output 返回的分頁Html字符串  94      */
 95     public function showPageStringAsAJAX( $jumpFun, $currentPage, $pageSize, $totalCount){  96         
 97         $pageSize = $pageSize == 0 ? 3 : $pageSize;  98         
 99         $totalPages = floor(($totalCount + $pageSize - 1) / $pageSize); //總頁數
100         $output = '<nav><ul class="pagination pagination-lg">';//輸出字符串
101         if ($totalPages > 1) 102  { 103             //if (currentPage != 1)
104             {//處理首頁連接
105                 $output .= "<li><a class='pageLink' href='javascript:void(0);'  onclick='".$jumpFun."(1,".$pageSize.")'>首頁</a></li>"; 106         
107  } 108             if ($currentPage > 1) 109             {//處理上一頁的連接
110                 $output .= "<li><a href='javascript:void(0);' onclick='".$jumpFun."(".($currentPage - 1).", ".$pageSize.")'>上一頁</a></li>"; 111  } 112             else
113  { 114                 // output.Append("<span class='pageLink'>上一頁</span>");
115  } 116         
117             $output .= " "; 118             $currint = 5; 119             for ($i = 0; $i <= 10; $i++) 120             {//一共最多顯示10個頁碼,前面5個,后面5個
121                 if (($currentPage + $i - $currint) >= 1 && ($currentPage + $i - $currint) <= $totalPages) 122  { 123                     if ($currint == $i) 124                     {//當前頁處理 125  //output.Append(string.Format("[{0}]", currentPage));
126                         $output .= "<li class='active'><a  href='javascript:void(0);'>$currentPage</a></li>"; 127  } 128                     else
129                     {//一般頁處理
130                         $output .= "<li><a  href='javascript:void(0);' onclick='".$jumpFun."(".($currentPage + $i - $currint).",".$pageSize.")'>".($currentPage + $i - $currint)."</a></li>"; 131  } 132  } 133                 $output .= " "; 134  } 135             if ($currentPage < $totalPages) 136             {//處理下一頁的鏈接
137                 $output .= "<li><a  href='javascript:void(0);' onclick='".$jumpFun."(".($currentPage + 1).",".$pageSize.")'>下一頁</a></li>"; 138  } 139             else
140  { 141                 //output.Append("<span class='pageLink'>下一頁</span>");
142  } 143             $output .=" "; 144             if ($currentPage != $totalPages) 145  { 146                 //處理末頁的鏈接
147                 $output .= "<li><a  href='javascript:void(0);' onclick='".$jumpFun."(".$totalPages.", ".$pageSize.")'>末頁</a></li>"; 148  } 149             $output .= "</ul></nav>"; 150  } 151         //$output .= "第".$currentPage."頁 / 共".$totalPages."頁";//這個統計加不加都行
152         
153         return $output; 154  } 155     
156 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM