首页 > 代码库 > PHP分页类,生成分页html字符串

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="http://www.mamicode.com/.$redirectTo."?pageIndex=1&pageSize=".$pageSize."‘>首页</a></li>"; 36                  37             } 38             if ($currentPage > 1) 39             {//处理上一页的连接 40                 $output .= "<li><a  href=http://www.mamicode.com/‘".$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=http://www.mamicode.com/‘".$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=http://www.mamicode.com/‘".$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=http://www.mamicode.com/‘".$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             else113             {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                     else129                     {//一般页处理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             else140             {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 }

 

PHP分页类,生成分页html字符串