我工作的一个PHP分页算法。我可以猜到,它需要改进的余地,所以我想就如何改进它,无论是清理$几点思考C $ C本身,从UI / UX的角度来看,或其他任何你能想到的。
看起来像这样的算法应该输出分页:
1 2 3 ... 6 7 8 ... 97 98 99
或者这样的:
1 2 3 4 5 ... 6 7 8 9 10
或者这样的:
1 2 3
这是我的code:
< PHP
如果(!is_null($ _ GET ['页'])){
$当前页= $ _GET ['页'];
} 其他 {
$当前是= 1;
}
如果($页!= NULL){
回声页面:';
}
//不到10页
如果($页< = 10){
为($页= 1; $页< = $页; $页++){
回声'< A HREF =页面=?''&GT $页。; 。 $页面。 '&所述; / a取代; ';
}
//超过10页大,而我们是在他们中间的某个地方
} elseif的(($页→10)及及($当前页→4)及及($当前第&所述; $页 - 3)){
为($页= 1; $页< = 3; $页++){
回声'< A HREF =页面=?''&GT $页。; 。 $页面。 '&所述; / a取代; ';
}
回声 '... ';
为($页= $当前页 - 1; $页< = $当前页+ 1; $页++){
回声'< A HREF =页面=?''&GT $页。; 。 $页面。 '&所述; / a取代; ';
}
回声 '... ';
为($页= $页 - 2; $页< = $页; $页++){
回声'< A HREF =页面=?''&GT $页。; 。 $页面。 '&所述; / a取代; ';
}
//大于10页,而且我们对网页的结束
} 其他 {
为($页= 1; $页< = 5; $页++){
回声'< A HREF =页面=?''&GT $页。; 。 $页面。 '&所述; / a取代; ';
}
回声 '... ';
为($页= $页 - 5; $页< = $页; $页++){
回声'< A HREF =页面=?''&GT $页。; 。 $页面。 '&所述; / a取代; ';
}
}
解决方案
我不知道如果我的code是任何比你的好,但这里是我如何解决类似的问题。
这需要对页面生成量的参数,并与类创建一个div 页
包含锚,当前页面有一个类电流。该解决方案似乎有点清洁剂,因为有较少的重复。
函数generate_pages($总,$电流)
{当传递总页数输出//将生成的网页,并链接到?页= X
如果($总数大于1)
{
$总额= INTVAL($总);
$输出='< DIV CLASS =页面>';
$ current_page =(假==使用isset($电流))? 1:$电流;
为($页= 1; $页< $共+ 1; $页++)
{
$较低= $ current_page-3;
$上限= $ current_page + 3;
$特殊=($页== $ current_page)? 类= \当前\:;
如果(($页> $低级放大器;&安培; $页< $上)|| $第2 || $页>($总-1))
{
如果($ last_done_page + 1 = $页!)$输出='...'。
$输出='<一'。$特殊。 HREF =页面='$页。'?>&所述'$页。'; / a取代;';
$ last_done_page = $页;
}
}
$输出='< / DIV>。
返回$输出;
}
}
I'm working on a pagination algorithm in 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.
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
. 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;
}
}