分頁
基本
<?php
function pages($pnumrow,$p,$knum=20)
{
echo '<nav style="margin: 10px 50px;text-align: center;">
<ul class="pagination">';
$pg=ceil($pnumrow/$knum);
echo '<li><a href="works.php?p=1"><span aria-hidden="true">«</span>
<span class="sr-only">Previous</span></a></li>';
if($pg>0)
{
for ($i=1; $i <= $pg; $i++) {
if(abs($i-$p)<=5){
echo '<li class="',($i==$p)?'active':'','"><a href="works.php?p=',$i,'">',$i,'</a></li>';
}
}
}
echo '<li><a href="works.php?p=',$pg,'"><span aria-hidden="true">»</span>
<span class="sr-only">Next</span></a></li>';
echo '</ul>
</nav>';
}
改良
class Cpage
{
var $url, $num, $each, $arg, $page, $end;
function __construct($url='#',$num=0,$page=1,$args=Array(),$each = 20,$end=1)
{
$this->url = $url ;
$this->num = $num ;
$this->each = $each ;
$this->page = ($page>1)?intval($page):1 ;
$this->end = $end ;
$arg='?';
foreach ($args as $key => $v) {
if($v===''||$v===null){continue;}
$arg=$arg.$key.'='.$v.'&';
}
$this->arg = $arg ;
}
public function sum(){
$nu=floor($this->num/$this->each);
$this->end=($this->num%$this->each==0)?($nu):($nu+1);
return $this;
}
public function show(){
echo '<!--分頁-->
<div class="row datatables-footer">
<div class="col-sm-12 col-md-6">
<div class="dataTables_info" id="datatable-editable_info" role="status" aria-live="polite">
資料筆數: ' , $this->num , ' 筆 <1 - ',$this->end,'>
</div>
</div>';
if($this->num>0){
echo '<div class="col-sm-12 col-md-6">
<div class="dataTables_paginate paging_bs_normal" id="datatable-editable_paginate">
<ul class="pagination">';
echo '<li class="prev ',($this->page==1)?'disabled':'','"><a href="',$this->url,$this->arg,'&p=1"><span class="fa fa-chevron-left"></span></a></li>';
for($i=1; $i<=$this->num; $i++){
if($i<=($this->page+5)||$i>=($this->page-5)){
echo '<li ',($i==$this->page)?'class="active"':'','><a href="',$this->url,$this->arg,'&p=',$i,'">',$i,'</a></li>';
}
}
echo '<li class="next ',($this->page==$this->end)?'disabled':'','"><a href="',$this->url,$this->arg,'&p=',$this->end,'"><span class="fa fa-chevron-right"></span></a></li>';
echo '</ul>
</div>';
}
echo '</div>
</div>
<!--分頁 END-->';
}
}