PHP Pagination前一个按钮/链接

时间:2013-09-12 15:22:08

标签: php pagination

我从W£Schools网站获得了一个完整的分页系统。我定制它,它没有错。

我想知道是否有一种简单的方法可以在列表的开头添加“上一个”链接。试图在当前页面上通过-1进行,但它打破了其余的分页。

这是我目前的工作代码:

$adjacents = "2";

$page = ($page == 0 ? 1 : $page); 
$start = ($page - 1) * $per_page; 

$prev = $page - 1; 
$next = $page + 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1;

$pagination = "";
if($lastpage > 1)
{ 
$pagination .= "<ul>";
//$pagination .= "<li class='details'>Page $page of $lastpage</li>";
if ($lastpage < 7 + ($adjacents * 2)) { 
    for ($counter = 1; $counter <= $lastpage; $counter++)
    {
        if ($counter == $page)
        $pagination.= "<li><a class='current'>$counter</a></li>";
        else
        $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>"; 
    }
}
elseif($lastpage > 5 + ($adjacents * 2)) {
    if($page < 1 + ($adjacents * 2)) {
        for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
        {
            if ($counter == $page) {
                $pagination.= "<li><a class='active'>$counter</a></li>";
            } else {
                $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>"; 
            }
        }
        $pagination.= "<li class='dot'>...</li>";
        $pagination.= "<li><a href='{$url}$lpm1'>$lpm1</a></li>";
        $pagination.= "<li><a href='{$url}$lastpage'>$lastpage</a></li>"; 
    }
    elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
        $pagination.= "<li><a href='{$url}1'>1</a></li>";
        $pagination.= "<li><a href='{$url}2'>2</a></li>";
        $pagination.= "<li class='dot'>...</li>";
        for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
        {
            if ($counter == $page) {
                $pagination.= "<li><a class='active'>$counter</a></li>";
            } else {
                $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>"; 
            }
        }
        $pagination.= "<li class='dot'>..</li>";
        $pagination.= "<li><a href='{$url}$lpm1'>$lpm1</a></li>";
        $pagination.= "<li><a href='{$url}$lastpage'>$lastpage</a></li>";                                   
    } else {
        $pagination.= "<li><a href='{$url}1'>1</a></li>";
        $pagination.= "<li><a href='{$url}2'>2</a></li>";
        $pagination.= "<li class='dot'>..</li>";
        for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
        {
            if ($counter == $page) {
                $pagination.= "<li><a class='current'>$counter</a></li>";
            } else {
                $pagination.= "<li><a href='{$url}$counter'>$counter</a></li>"; 
            }
        }
    }
    if ($page < $counter - 1) {
    $pagination.= "<li><a href='{$url}$next'>Next</a></li>";
    // $pagination.= "<li><a href='{$url}$lastpage'>Last</a></li>";
    }else{
    //$pagination.= "<li><a class='current'>Next</a></li>";
    // $pagination.= "<li><a class='current'>Last</a></li>";
    }
    $pagination.= "</ul>\n"; 
} 
return $pagination;
} 

$page=1;//Default page
$limit=26;//Records per page
$start=0;//starts displaying records from 0
if(isset($_GET['page']) && $_GET['page']!=''){
$page=$_GET['page'];
}
$start=($page-1)*$limit;

1 个答案:

答案 0 :(得分:0)

您已经有一个prev变量。你只需要添加它。

$pagination.= "<li><a href='{$url}$prev'>Prev</a></li>";

将其作为您的第一个li元素。一句警告,你需要一个if声明,否则它会在你前一页1

时中断