本文介绍了停留在显示PHP分页器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个分页器.我创建了一个带有以下属性的分页器类.但是现在我试图显示分页器,但是我完全陷入了不同的循环(也许我应该去睡觉:P)和其他东西.

I'm trying to create a paginator. I have created a paginator class with properties you can see below. But now I'm trying to display the paginator, but I'm completely stuck on different loops (perhaps I should just go to bed :P) and stuff.

重要属性是:

windows_size // the amount of pages visible
num_pages // the total number of pages
current_page // the currently selected page

我有以下对象:

o: (Paginator)
 ->item_count  : 295
   page_size   : 50
   window_size : 5
   num_pages   : 10
   current_page: 1
   first_index : 50

我想显示:

[1] 2 3 4 5 ...

[1] 2 3 4 5 ...

或者我有

o: (Paginator)
 ->item_count  : 295
   page_size   : 50
   window_size : 5
   num_pages   : 10
   current_page: 5
   first_index : 50

... 3 4 [5] 6 7 ...

... 3 4 [5] 6 7...

或者我有

o: (Paginator)
 ->item_count  : 295
   page_size   : 50
   window_size : 5
   num_pages   : 10
   current_page: 9
   first_index : 50

... 6 7 8 [9] 10

... 6 7 8 [9] 10

我如何显示它?

最终结果(非常感谢:Wh1T3h4Ck5!)

END RESULTS (many thanks to: Wh1T3h4Ck5!)

http://pastebin.com/2KzL1Tn6

推荐答案

通常,这是一段非常简单的代码.

Generally, it's very simple piece of code.

您可以使用类似这样的东西.

You can use something like this.

function myPaginator($total_items, $items_per_page, $visible_pages_pad, $active_page) {
  $res = '';
  if ($items_per_page > 0 && $active_page > 0 && $visible_pages_pad) {
  $max_page = floor($total_items/$items_per_page);
  if ($total_items % $items_per_page > 0) $max_page++;
    if ($active_page <= $max_page) {
      $visible_pages = $visible_pages_pad * 2 + 1;
      if ($max_page <= $visible_pages) {
        $minp = 1;
        $maxp = $max_page;
        }
      else {
        $minp = $active_page - $visible_pages_pad;
        if ($minp < 1) $minp = 1;
        $maxp = $minp + $visible_pages - 1;
        if ($maxp > $max_page) {
          $maxp = $max_page;
          $minp = $maxp - $visible_pages + 1;
          }
        }
      for ($i = $minp; $i <= $maxp; $i++) {
        $page = $i == $active_page ? "[$i]" : $i; // create links here
        $res .= "$page ";
        }
      $res = trim($res);
      if ($minp > 1) $res = '... ' . $res; // left dots
      if ($maxp < $max_page) $res .= ' ...'; // right dots
      }
    }
  return $res == '' ? false : $res;
  }


参数:

  • $ total_items -列表中的项目总数
  • $ items_per_page -您希望页面上的菜单项如何
  • $ visible_pages_pad -左/右填充(此值* 2 +1 =分页器项的数量)
  • $ active_page -在分页器中选择的页面
  • $total_items - total number of items in list
  • $items_per_page - how menu items you want on page
  • $visible_pages_pad - left/right padding (this value * 2 + 1 = number of paginator items)
  • $active_page - selected page in paginator


结果:


限制:

  • 所有参数都应为整数
  • $ items_per_page> 0
  • $ visible_pages_pad> 0
  • 1< = $ active_page< = {页面总数}


用法:

$total_items = 250;      // total items
$items_per_page = 20;    // 250/20 = 13 pages
$visible_pages_pad = 2;  // 5 pages visible in paginator (2*2+1)

$active_page = 1;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: [1] 2 3 4 5 ...

$active_page = 2;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: 1 [2] 3 4 5 ...

$active_page = 3;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: 1 2 [3] 4 5 ...

$active_page = 6;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: ... 4 5 [6] 7 8 ...

$active_page = 11;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: ... 9 10 [11] 12 13

$active_page = 12;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: ... 9 10 11 [12] 13

$active_page = 13;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: ... 9 10 11 12 [13]

注意:$ visible_pages_pad是活动页数左侧/右侧的分页器中的页数.

Note: $visible_pages_pad is number of pages in paginator left/right of the active-page number.

希望这对您有帮助...

Hope this helps...

这篇关于停留在显示PHP分页器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:42