简单php原生分页条制作方法
//根据分页参数,得到分页数据 public function getPageParam($currentpage,$totalcount,$pagesize=10,$pm='',$pagerange=3){ $totalpage=0; //总页数 $result = [ 'totalrow' => "<a class='totalrow'>共 {$totalcount} 条记录</a>", 'pagesize' => "<a class='pagesize'>每页{$pagesize}条", 'totalpage' => "<a class='totalrow'>共 1 页</a>", 'prev' => '', 'first' => '', 'pleft' => '', //左侧省略号 'left' => [], //左侧分页 'current' => '', 'right' => [], 'pright' => '', 'end' => '', //最后一页 'next' => '' ]; $raw = [ 'page' => $currentpage, 'pagesize' => $pagesize, 'pagerange' => $pagerange, 'totalcount' => $totalcount, 'totalpage' => 1 ]; if($totalcount < 1){ $result['totalpage'] = "<a class='totalrow'>共 0 页</a>"; $raw['totalpage'] = 0; return ['list'=>$result,'raw'=>$raw]; } if($totalcount<=$pagesize){ return ['list'=>$result,'raw'=>$raw]; } //总页数 $totalpage = ceil($totalcount/$pagesize); $raw['totalpage'] = $totalpage; $startpage=$currentpage-$pagerange>0?$currentpage-$pagerange:1; $endpage=$currentpage+$pagerange<$totalpage?$currentpage+$pagerange:$totalpage; //参数绑定 $canshu = array(); if(is_array($pm) && count($pm)>0){ $canshu = $pm; } //上一页 if($currentpage > 1){ $tmp = $canshu; $tmp[]="p=" . ($startpage - 1); $cs = implode("&",$tmp); $result['prev'] = "<a class='prev' href='?{$cs}'><</a>"; } //第一页、省略号 if($startpage > 1){ $tmp = $canshu; $tmp[]="p=1"; $cs = implode("&",$tmp); $result['first'] = "<a class='first' href='?{$cs}'>1</a>"; $result['pleft'] = "<a class='pleft'>···</a>"; } //当前分页前 for($i=$startpage;$i<$currentpage;$i++){ $tmp = $canshu; $tmp[]="p=$i"; $cs = implode("&",$tmp); $result['left'][] = "<a class='left' href='?{$cs}'>$i</a>"; } //当前分页 $result['current'] = '<a class="current">'.$currentpage.'</a>'; //当前分页后 for($i=$currentpage+1;$i<=$endpage;$i++){ $tmp = $canshu; $tmp[]="p=$i"; $cs = implode("&",$tmp); $result['right'][] = "<a class='right' href='?{$cs}'>$i</a>"; } //省略号、最后1页 if($totalpage > $endpage){ $tmp = $canshu; $tmp[]="p=" . $totalpage; $cs = implode("&",$tmp); $result['pright'] = "<a class='pright'>···</a>"; $result['end'] = "<a class='end' href='?{$cs}'>{$totalpage}</a>"; } //下一页 if($totalpage > $currentpage){ $tmp = $canshu; $tmp[]="p=" . ($endpage + 1); $cs = implode("&",$tmp); $result['next'] = "<a class='next' href='?{$cs}'>></a>"; } return ['list'=>$result,'raw'=>$raw]; } //得到分页条HTML源码 //当前分页 //总记录数 //每页显示多少条 //页面参数,字符或数组['year=2021','sex=1'] //也宽,页码显示跨度 public static function getPageHtml2($currentpage,$totalcount,$pagesize=10,$pm="",$pagerange=3){ $result = self::getPageParam($currentpage,$totalcount,$pagesize,$pm,$pagerange); $arr = []; foreach($result['list'] as $k=>$v){ if(is_array($v)) $arr[] = implode('',$v); else $arr[] = $v; } return '<div class="pages">'. implode('',$arr).'</div>'; }
调用的时候直接CLASS::getPageHtml2(5,100,10);即可
给个简单样式参考
/*分页*/ .pages{ text-align:center; background:#ffffff; } .pages a{ display:inline-block; font-size:13px; text-decoration:none; padding:6px 6px; line-height:1em; text-align:center; min-width:22px; margin:0 3px; background-color:rgb(244,244,244); color:#333333; border-radius:3px; user-select:none; } .pages a.current{ background-color:rgb(152,39,43); color:#ffffff; }