我有分页链接列表,该列表将显示前5个分页链接,其限制为20,如果用户选择5个限制,则需要限制分页链接。如何在php中实现它?我尝试使用以下代码,请根据用户选择的限制让我对其进行优化。
$limit = 20;
$total = 156;
$total_pages = ceil($total / $limit);
$current_page = $params['page']; //current page
if($current_page==1) {
$paging .= "<li class='association_page mgn-previous' style='display:none !important;' page='0'>Previous</li>";
} else{
$paging .= "<li class='association_page mgn-previous' style='display:block !important;' page='".$previous_page."'>Previous</li>";
}
for ($i=1; $i<=$total_pages; $i++) {
if($current_page == $i) {
$paging .= "<li class='association_page current' >".$i."</li>";
} else {
$paging .= "<li class='association_page' >".$i."</li>";
}
}
if($total_pages==$current_page){
$paging .= "<li class='association_page mgn_next' style='display:none !important' page='".$page_next."' >Next</li>";
} else {
$paging .= "<li class='association_page mgn_next' page='".$page_next."' >Next</li>";
}
//输出将显示
1 2 3 5 next
after 12345678
if user choose 5 it goes on 1 2 3 4 5 5 32
how to reduce it