简体   繁体   English

如何改进这种PHP分页算法?

[英]How can I improve this PHP pagination algorithm?

I'm working on a pagination algorithm in PHP. 我正在研究PHP中的分页算法。 I can guess that it needs room for improvement, so I'd like some thoughts on how to improve it, be it cleaning up the code itself, from a UI/UX standpoint, or anything else you can think of. 我可以猜测它需要改进的空间,所以我想要一些关于如何改进它的想法,无论是从UI / UX角度清理代码本身,还是你能想到的任何其他东西。

The algorithm should output pagination that looks like this: 该算法应输出如下所示的分页:

1 2 3 ... 6 7 8 ... 97 98 99

or this: 或这个:

1 2 3 4 5 ... 6 7 8 9 10

or this: 或这个:

1 2 3

Here's my code: 这是我的代码:

<?php

if(!is_null($_GET['page'])) {
  $currentPage = $_GET['page'];
} else {
  $currentPage = 1;
}

if($pages != null) {
  echo 'Page: ';
}

// Less than 10 pages
if($pages <= 10) {
  for($page = 1; $page <= $pages; $page++) {
    echo '<a href="?page=' . $page . '">' . $page . '</a> ';
  }

// Greater than 10 pages, and we're somewhere in the middle of them
} elseif(($pages > 10) && ($currentPage > 4) && ($currentPage < $pages - 3)) {
  for($page = 1; $page <= 3; $page++) {
    echo '<a href="?page=' . $page . '">' . $page . '</a> ';
  }
  echo '... ';
  for($page = $currentPage - 1; $page <= $currentPage + 1; $page++) {
    echo '<a href="?page=' . $page . '">' . $page . '</a> ';
  }
  echo '... ';
  for($page = $pages - 2; $page <= $pages; $page++) {
    echo '<a href="?page=' . $page . '">' . $page . '</a> ';
  }

// Greater than 10 pages, and we're towards the end of the pages
} else {
  for($page = 1; $page <= 5; $page++) {
    echo '<a href="?page=' . $page . '">' . $page . '</a> ';
  }
  echo '... ';
  for($page = $pages - 5; $page <= $pages; $page++) {
    echo '<a href="?page=' . $page . '">' . $page . '</a> ';
  }
}

I'm not sure if my code is any better than yours, but here is how I solved a similar problem. 我不确定我的代码是否比你的更好,但这是我解决类似问题的方法。

It takes a parameter for the amount of pages to generate and creates a div with the class pages containing anchors, the current page has a class of current . 它需要一个参数来生成页面数量,并使用包含锚点的类pages创建一个div,当前页面有一个current类。 This solution seems a little cleaner because there is less repetition. 这个解决方案似乎更清洁,因为重复次数更少。

function generate_pages($total,$current)
{ //Will generate pages and link to ?page=x when passed total pages to output
    if($total > 1)
    {
        $total=intval($total);

        $output='<div class="pages">';
        $current_page= (false == isset($current)) ? 1 : $current;
        for($page=1;$page<$total+1;$page++)
        {
            $lower=$current_page-3;
            $upper=$current_page+3;
            $special = ($page==$current_page) ? " class=\"current\"" : "";
            if(($page > $lower && $page < $upper) || $page < 2 || $page > ($total-1))
            {
                if($last_done_page+1 != $page) $output.= '... ';
                $output.='<a'.$special.' href="?page='.$page.'">'.$page.'</a>';
                $last_done_page=$page;
            }
        }
        $output.='</div>';
        return $output;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM