php自定义分页
最终效果:
css 样式
/*pages*/ .pages{ box-sizing:content-box; display: inline-block; border-top: 1px solid #e6e6e6; border-bottom: 1px solid #e6e6e6; border-left: 1px solid #e6e6e6; } .pages a,.pages .curr{ border-right: 1px solid #e6e6e6; display: inline-block; text-align: center; padding: 2px 10px; float: left; } .pages .curr{ background-color: #009688; color: #fff; cursor: not-allowed } .pages .disable{ cursor: not-allowed; }
php代码(作为公共函数使用,作为类使用也可以)
/** * 分页html * @param int $curPage 当前页 * @param int $totalPage 总页数 * @param int $url 地址+传递参数 * @return string * */ function pages_html($curPage,$totalPage,$url=''){ $string =""; $prev = $curPage-1; $next = $curPage+1; if($curPage-1<1){ $prev = 1; $string .= "<a href='javascript:void(0)' class='prev disable'>上一页</a>"; }else{ $string .= "<a href='{$url}&page={$prev}' class='prev'>上一页</a>"; } for($i=1;$i<=$totalPage;$i++){ if($i==$curPage){ // 当前页 $string .= "<a href='javascript:void(0)' class='curr'>$i</a>"; }else{ $string .= "<a href='{$url}&page={$i}'>$i</a>"; } } if($next-1>=$totalPage){ $next = $totalPage; $string .= "<a href='javascript:void(0)' class='next disable'>下一页</a>"; }else{ $string .= "<a href='{$url}&page={$next}' class='next'>下一页</a>"; } return $string; }
php调用:
$assign['pages'] = pages_html($assign['curPage'],$assign['totalPage'],url('purchase/index')); // url传参
return $this->view('',$assign);