Example #1
0
 /**
  * @param int $current_page
  * @param int $total_size
  * @param int $page_size
  * @param int $mode
  * @return string
  */
 public static function get_pages_html($current_page, $total_size, $page_size = 10, $mode = 1)
 {
     $get_vars = Lib_Request::get_vars();
     $querystr = '';
     if (isset($get_vars['page'])) {
         unset($get_vars['page']);
     }
     if (!empty($get_vars)) {
         foreach ($get_vars as $k => $v) {
             if (empty($v)) {
                 continue;
             }
             $querystr .= $k . "=" . $v . "&";
         }
     }
     $mode = $mode > 0 ? $mode : 1;
     $num = $mode * 2 + 1;
     $current_page = $current_page < 1 ? 1 : $current_page;
     $current_url = Module_HttpRequest_Router::current_url_path();
     $current_url .= "?" . $querystr;
     $pages_html = '';
     $total_pages = ceil($total_size / $page_size);
     $current_page = $current_page > $total_pages ? $total_pages : $current_page;
     $pages_html .= '<ul class="pagination"><li><span>总计' . $total_size . '条记录&nbsp; 共' . $total_pages . '页</span></li></ul>&nbsp; ';
     if ($total_size < $page_size) {
         return $pages_html;
     }
     $pages_html .= '<ul class="pagination">';
     //
     if ($current_page <= 1) {
         $pages_html .= '<li class="disabled"><span>上一页</span></li>';
     } else {
         $pages_html .= '<li><a href="' . $current_url . 'page=' . ($current_page - 1) . '">上一页</a></li>';
     }
     //
     if ($total_pages <= $num + 2) {
         for ($i = 1; $i <= $total_pages; $i++) {
             $pages_html .= $i == $current_page ? '<li class="active"><span>' . $i . '</span></li>' : '<li><a href="' . $current_url . 'page=' . $i . '">' . $i . '</a></li>';
         }
     } else {
         $page_start = $current_page - $mode;
         $page_start = $page_start < 2 ? 2 : $page_start;
         $pages_html .= $current_page == 1 ? '<li class="active"><span>1</span></li>' : '<li><a href="' . $current_url . 'page=1">1</a></li>';
         if ($page_start > 2 && $total_pages > $num) {
             $pages_html .= '<li><span>...</span></li>';
         }
         $num = $page_start + $num;
         for ($i = $page_start; $i < $num; $i++) {
             if ($i > $total_pages) {
                 break;
             }
             $pages_html .= $i == $current_page ? '<li class="active"><span>' . $i . '</span></li>' : '<li><a href="' . $current_url . 'page=' . $i . '">' . $i . '</a></li>';
         }
         if ($num < $total_pages) {
             $pages_html .= '<li><span>...</span></li>';
         }
         if ($num <= $total_pages) {
             $pages_html .= $current_page == $total_pages ? '<li class="active"><span>' . $total_pages . '</span></li>' : '<li><a href="' . $current_url . 'page=' . $total_pages . '">' . $total_pages . '</a></li>';
         }
     }
     //
     if ($current_page >= $total_pages) {
         $pages_html .= '<li class="disabled"><span>下一页</span></li>';
     } else {
         $pages_html .= '<li><a href="' . $current_url . 'page=' . ($current_page + 1) . '">下一页</a></li>';
     }
     $pages_html .= '</ul>';
     return $pages_html;
 }