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

如何改进这个PHP分页算法?,php,algorithm,user-interface,pagination,Php,Algorithm,User Interface,Pagination,我正在用PHP开发一个分页算法。我可以猜测它需要改进,所以我想了解一些如何改进它的想法,从UI/UX的角度来看,无论是清理代码本身,还是你能想到的任何东西 算法应输出如下所示的分页: 1 2 3 ... 6 7 8 ... 97 98 99 或者这个: 1 2 3 4 5 ... 6 7 8 9 10 或者这个: 1 2 3 这是我的代码: <?php if(!is_null($_GET['page'])) { $currentPage = $_GET['page']; } e

我正在用PHP开发一个分页算法。我可以猜测它需要改进,所以我想了解一些如何改进它的想法,从UI/UX的角度来看,无论是清理代码本身,还是你能想到的任何东西

算法应输出如下所示的分页:

1 2 3 ... 6 7 8 ... 97 98 99
或者这个:

1 2 3 4 5 ... 6 7 8 9 10
或者这个:

1 2 3
这是我的代码:

<?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> ';
  }
}

我不确定我的代码是否比你的好,但下面是我如何解决类似问题的

它接受一个用于生成页数的参数,并使用包含锚定的类
pages
创建一个div,当前页面的类为
current
。这个解决方案看起来更干净一些,因为重复次数更少

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;
    }
}
函数生成页面($total,$current)
{//将总页面传递到输出时,将生成页面并链接到?page=x
如果($total>1)
{
$total=intval($total);
$output='';
$current_page=(false==isset($current))?1:$current;
对于($page=1;$page$lower&$page<$upper)| |$page<2 | |$page>($total-1))
{
如果($last_done_page+1!=$page)$output.='…';
$output.='';
$last_done_page=$page;
}
}
$output.='';
返回$output;
}
}

@Yehonatan,你介意稍微有点建设性吗?这篇文章中的另一个很好的分页功能虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-您需要提取函数中的全局值(
$\u GET['page']
),因为它不是应该处理这种逻辑的视图层。改为将其作为函数的参数,否则您已锁定应用程序。无法将变量
$\u GET['page']
用于其他任何不可移植的对象。