效果图:
代码
<style type="text/css">
<!--
.disabled a{color:#ccc;}
>
-->
</style>
<?php
$dsn = "mysql:host=192.168.0.113;dbname=portal";
$pdo= new PDO($dsn, 'dev', '123456');
$theurl="?p=example1";
$pageIndex=empty($_GET['pageIndex'])?1:intval($_GET['pageIndex'])==0?1:intval($_GET['pageIndex']); //当前页数
$perpage=15;//每页条数
$count=0;//总记录数
$resultArr=array();//查询结果数组
$start=0;//开始索引处
$multi="";//分页条码
$sql_c="SELECT count(*) FROM users";
//1.计算总条数:
$rs = $pdo->query($sql_c);
$count=$rs->fetchColumn();
if($count){
//1.开始索引处
$start=checkStart($count,$perpage,$pageIndex);
$sql="SELECT * FROM users LIMIT {$start},{$perpage}";
//echo $start;
//2.查询记录:
$rs = $pdo->query($sql);
if($rs){
$resultArr = $rs->fetchAll();
}
$multi=page($count,$perpage,$pageIndex,$theurl);
}
//计算记录开始索引处:
function checkStart($count,$perpage,$pageIndex){
if($perpage>=$count){
$pageIndex=1;
}
$pageCount=intval($count/$perpage);//总页数
//echo $pageCount;
if($count%$perpage!=0){
$pageCount=$pageCount+1;
}
if($pageIndex<0){
$pageIndex=1;
}
if($pageIndex>$pageCount){
$pageIndex=$pageCount;
}
return ($pageIndex-1)*$perpage;
}
//输出分页函数:
function page($count,$perpage,$pageIndex,$theurl){
if($perpage>=$count){
return ""; //直接短路,不输出分页条码
$pageIndex=1;
}
$pageCount=intval($count/$perpage);//总页数
if($count%$perpage!=0){
$pageCount=$pageCount+1;
}
if($pageIndex<0){
$pageIndex=1;
}
if($pageIndex>$pageCount){
$pageIndex=$pageCount;
}
//链接控制:
$firstPage=$previousPage=$nextPage=$lastPage="1";
$firstPage=1;
if($pageIndex==1){
$previousPage=1;
}else{
$previousPage=$pageIndex-1;
}
if($pageIndex==$pageCount){
$nextPage=$pageCount;
}else{
$nextPage=$pageIndex+1;
}
$lastPage=$pageCount;
//
$firstUrl=$theurl."&pageIndex=".$firstPage;
$previousUrl=$theurl."&pageIndex=".$previousPage;
$nextUrl=$theurl."&pageIndex=".$nextPage;
$lastUrl=$theurl."&pageIndex=".$lastPage;
//
//$outStr="<span><a href='".Url($firstUrl,false)."'>首页</a></span>|<span><a href='".Url($previousUrl,false)."'>上一页</a></span>|<span><a href='".Url($nextUrl,false)."'>下一页</a></span>|<span><a href='".$lastUrl."'>末页</a></span>";
$outStr="<span><a href='".$firstUrl."'>首页</a></span>|<span><a href='".$previousUrl."'>上一页</a></span>|<span><a href='".$nextUrl."'>下一页</a></span>|<span><a href='".$lastUrl."'>末页</a></span>";
return $outStr;
}
if($resultArr){
foreach($resultArr as $key=>$value){
echo "<p>".$value['Username']."</p>";
}
echo "<br/>",($multi);
}else{
echo("没有数据");
}
?>
<style type="text/css">
<!--
.disabled a{color:#ccc;}
>
-->
</style>
<?php
$dsn = "mysql:host=192.168.0.113;dbname=portal";
$pdo= new PDO($dsn, 'dev', '123456');
$theurl="?p=example1";
$pageIndex=empty($_GET['pageIndex'])?1:intval($_GET['pageIndex'])==0?1:intval($_GET['pageIndex']); //当前页数
$perpage=15;//每页条数
$count=0;//总记录数
$resultArr=array();//查询结果数组
$start=0;//开始索引处
$multi="";//分页条码
$sql_c="SELECT count(*) FROM users";
//1.计算总条数:
$rs = $pdo->query($sql_c);
$count=$rs->fetchColumn();
if($count){
//1.开始索引处
$start=checkStart($count,$perpage,$pageIndex);
$sql="SELECT * FROM users LIMIT {$start},{$perpage}";
//echo $start;
//2.查询记录:
$rs = $pdo->query($sql);
if($rs){
$resultArr = $rs->fetchAll();
}
$multi=page($count,$perpage,$pageIndex,$theurl);
}
//计算记录开始索引处:
function checkStart($count,$perpage,$pageIndex){
if($perpage>=$count){
$pageIndex=1;
}
$pageCount=intval($count/$perpage);//总页数
//echo $pageCount;
if($count%$perpage!=0){
$pageCount=$pageCount+1;
}
if($pageIndex<0){
$pageIndex=1;
}
if($pageIndex>$pageCount){
$pageIndex=$pageCount;
}
return ($pageIndex-1)*$perpage;
}
//输出分页函数:
function page($count,$perpage,$pageIndex,$theurl){
if($perpage>=$count){
return ""; //直接短路,不输出分页条码
$pageIndex=1;
}
$pageCount=intval($count/$perpage);//总页数
if($count%$perpage!=0){
$pageCount=$pageCount+1;
}
if($pageIndex<0){
$pageIndex=1;
}
if($pageIndex>$pageCount){
$pageIndex=$pageCount;
}
//链接控制:
$firstPage=$previousPage=$nextPage=$lastPage="1";
$firstPage=1;
if($pageIndex==1){
$previousPage=1;
}else{
$previousPage=$pageIndex-1;
}
if($pageIndex==$pageCount){
$nextPage=$pageCount;
}else{
$nextPage=$pageIndex+1;
}
$lastPage=$pageCount;
//
$firstUrl=$theurl."&pageIndex=".$firstPage;
$previousUrl=$theurl."&pageIndex=".$previousPage;
$nextUrl=$theurl."&pageIndex=".$nextPage;
$lastUrl=$theurl."&pageIndex=".$lastPage;
//
//$outStr="<span><a href='".Url($firstUrl,false)."'>首页</a></span>|<span><a href='".Url($previousUrl,false)."'>上一页</a></span>|<span><a href='".Url($nextUrl,false)."'>下一页</a></span>|<span><a href='".$lastUrl."'>末页</a></span>";
$outStr="<span><a href='".$firstUrl."'>首页</a></span>|<span><a href='".$previousUrl."'>上一页</a></span>|<span><a href='".$nextUrl."'>下一页</a></span>|<span><a href='".$lastUrl."'>末页</a></span>";
return $outStr;
}
if($resultArr){
foreach($resultArr as $key=>$value){
echo "<p>".$value['Username']."</p>";
}
echo "<br/>",($multi);
}else{
echo("没有数据");
}
?>
添加数字导航:
代码
<style type="text/css">
<!--
.disabled a{color:#ccc;}
a.current{border:1px solid blue;}
>
-->
</style>
<?php
$dsn = "mysql:host=192.168.0.113;dbname=portal";
$pdo= new PDO($dsn, 'dev', '123456');
$theurl="?p=example1";
$pageIndex=empty($_GET['pageIndex'])?1:intval($_GET['pageIndex'])==0?1:intval($_GET['pageIndex']); //当前页数
$perpage=2;//每页条数
$count=0;//总记录数
$resultArr=array();//查询结果数组
$start=0;//开始索引处
$multi="";//分页条码
$sql_c="SELECT count(*) FROM users";
//1.计算总条数:
$rs = $pdo->query($sql_c);
$count=$rs->fetchColumn();
if($count){
//1.开始索引处
//$start=Pagination::checkStart($count,$perpage,$pageIndex);
$start=checkStart($count,$perpage,$pageIndex);
$sql="SELECT * FROM users LIMIT {$start},{$perpage}";
//echo $start;
//2.查询记录:
$rs = $pdo->query($sql);
if($rs){
$resultArr = $rs->fetchAll();
}
//$multi=Pagination::page($count,$perpage,$pageIndex,$theurl);
$multi=page($count,$perpage,$pageIndex,$theurl);
}
//计算记录开始索引处:
function checkStart($count,$perpage,$pageIndex){
if($perpage>=$count){
$pageIndex=1;
}
$pageCount=intval($count/$perpage);//总页数
//echo $pageCount;
if($count%$perpage!=0){
$pageCount=$pageCount+1;
}
if($pageIndex<0){
$pageIndex=1;
}
if($pageIndex>$pageCount){
$pageIndex=$pageCount;
}
return ($pageIndex-1)*$perpage;
}
//输出分页函数:
function page($count,$perpage,$pageIndex,$theurl){
if($perpage>=$count){
return ""; //直接短路,不输出分页条码
$pageIndex=1;
}
$pageCount=intval($count/$perpage);//总页数
if($count%$perpage!=0){
$pageCount=$pageCount+1;
}
if($pageIndex<0){
$pageIndex=1;
}
if($pageIndex>$pageCount){
$pageIndex=$pageCount;
}
//1.文本导航、链接控制:
$firstPage=$previousPage=$nextPage=$lastPage="1";
$firstPage=1;
if($pageIndex==1){
$previousPage=1;
}else{
$previousPage=$pageIndex-1;
}
if($pageIndex==$pageCount){
$nextPage=$pageCount;
}else{
$nextPage=$pageIndex+1;
}
$lastPage=$pageCount;
//
$firstUrl=$theurl."&pageIndex=".$firstPage;
$previousUrl=$theurl."&pageIndex=".$previousPage;
$nextUrl=$theurl."&pageIndex=".$nextPage;
$lastUrl=$theurl."&pageIndex=".$lastPage;
//2.数字导航:
$digitNav="数字导航";
$middle_d=$pageIndex;
$left1_d=$pageIndex-1;
$left2_d=$pageIndex-2;
$left3_d=$pageIndex-3;
$right1_d=$pageIndex+1;
$right2_d=$pageIndex+2;
$right3_d=$pageIndex+3;
$middle_s=$left1_s=$left2_s=$left3_s=$right1_s=$right2_s=$right3_s="";
$middle_s="<a class='current' href='".$theurl."&pageIndex=".$middle_d."'>$middle_d</a>";
if($left1_d>0){
$left1_s="<a href='".$theurl."&pageIndex=".$left1_d."'>$left1_d</a>";
}
if($left2_d>0){
$left2_s="<a href='".$theurl."&pageIndex=".$left2_d."'>$left2_d</a>";
}
if($left3_d>0){
$left3_s="<a>...</a>";
}
if($right1_d<=$pageCount){
$right1_s="<a href='".$theurl."&pageIndex=".$right1_d."'>$right1_d</a>";
}
if($right2_d<=$pageCount){
$right2_s="<a href='".$theurl."&pageIndex=".$right2_d."'>$right2_d</a>";
}
if($right3_d<=$pageCount){
$right3_s="<a>...</a>";
}
$digitNav=$left3_s.$left2_s.$left1_s.$middle_s.$right1_s.$right2_s.$right3_s;
//3.输入跳转:
$inputNav="<input type='text' id='tzpage' value='".$pageIndex."' onkeydown=\"javascript:if(event.keyCode==13){document.getElementById('tzbtn').focus();}\"/> <input type='button' id='tzbtn' value='跳转' onclick=\"javascript:window.location.href='".$theurl."&pageIndex='+document.getElementById('tzpage').value;\"/>";
//
$outStr="<span><a href='".$firstUrl."'>首页</a></span>|<span><a href='".$previousUrl."'>上一页</a></span> ".$digitNav." <span><a href='".$nextUrl."'>下一页</a></span>|<span><a href='".$lastUrl."'>末页</a></span> ".$inputNav;
return $outStr;
}
if($resultArr){
foreach($resultArr as $key=>$value){
echo "<p>".$value['Username']."</p>";
}
echo "<br/>",($multi);
}else{
echo("没有数据");
}
?>
<style type="text/css">
<!--
.disabled a{color:#ccc;}
a.current{border:1px solid blue;}
>
-->
</style>
<?php
$dsn = "mysql:host=192.168.0.113;dbname=portal";
$pdo= new PDO($dsn, 'dev', '123456');
$theurl="?p=example1";
$pageIndex=empty($_GET['pageIndex'])?1:intval($_GET['pageIndex'])==0?1:intval($_GET['pageIndex']); //当前页数
$perpage=2;//每页条数
$count=0;//总记录数
$resultArr=array();//查询结果数组
$start=0;//开始索引处
$multi="";//分页条码
$sql_c="SELECT count(*) FROM users";
//1.计算总条数:
$rs = $pdo->query($sql_c);
$count=$rs->fetchColumn();
if($count){
//1.开始索引处
//$start=Pagination::checkStart($count,$perpage,$pageIndex);
$start=checkStart($count,$perpage,$pageIndex);
$sql="SELECT * FROM users LIMIT {$start},{$perpage}";
//echo $start;
//2.查询记录:
$rs = $pdo->query($sql);
if($rs){
$resultArr = $rs->fetchAll();
}
//$multi=Pagination::page($count,$perpage,$pageIndex,$theurl);
$multi=page($count,$perpage,$pageIndex,$theurl);
}
//计算记录开始索引处:
function checkStart($count,$perpage,$pageIndex){
if($perpage>=$count){
$pageIndex=1;
}
$pageCount=intval($count/$perpage);//总页数
//echo $pageCount;
if($count%$perpage!=0){
$pageCount=$pageCount+1;
}
if($pageIndex<0){
$pageIndex=1;
}
if($pageIndex>$pageCount){
$pageIndex=$pageCount;
}
return ($pageIndex-1)*$perpage;
}
//输出分页函数:
function page($count,$perpage,$pageIndex,$theurl){
if($perpage>=$count){
return ""; //直接短路,不输出分页条码
$pageIndex=1;
}
$pageCount=intval($count/$perpage);//总页数
if($count%$perpage!=0){
$pageCount=$pageCount+1;
}
if($pageIndex<0){
$pageIndex=1;
}
if($pageIndex>$pageCount){
$pageIndex=$pageCount;
}
//1.文本导航、链接控制:
$firstPage=$previousPage=$nextPage=$lastPage="1";
$firstPage=1;
if($pageIndex==1){
$previousPage=1;
}else{
$previousPage=$pageIndex-1;
}
if($pageIndex==$pageCount){
$nextPage=$pageCount;
}else{
$nextPage=$pageIndex+1;
}
$lastPage=$pageCount;
//
$firstUrl=$theurl."&pageIndex=".$firstPage;
$previousUrl=$theurl."&pageIndex=".$previousPage;
$nextUrl=$theurl."&pageIndex=".$nextPage;
$lastUrl=$theurl."&pageIndex=".$lastPage;
//2.数字导航:
$digitNav="数字导航";
$middle_d=$pageIndex;
$left1_d=$pageIndex-1;
$left2_d=$pageIndex-2;
$left3_d=$pageIndex-3;
$right1_d=$pageIndex+1;
$right2_d=$pageIndex+2;
$right3_d=$pageIndex+3;
$middle_s=$left1_s=$left2_s=$left3_s=$right1_s=$right2_s=$right3_s="";
$middle_s="<a class='current' href='".$theurl."&pageIndex=".$middle_d."'>$middle_d</a>";
if($left1_d>0){
$left1_s="<a href='".$theurl."&pageIndex=".$left1_d."'>$left1_d</a>";
}
if($left2_d>0){
$left2_s="<a href='".$theurl."&pageIndex=".$left2_d."'>$left2_d</a>";
}
if($left3_d>0){
$left3_s="<a>...</a>";
}
if($right1_d<=$pageCount){
$right1_s="<a href='".$theurl."&pageIndex=".$right1_d."'>$right1_d</a>";
}
if($right2_d<=$pageCount){
$right2_s="<a href='".$theurl."&pageIndex=".$right2_d."'>$right2_d</a>";
}
if($right3_d<=$pageCount){
$right3_s="<a>...</a>";
}
$digitNav=$left3_s.$left2_s.$left1_s.$middle_s.$right1_s.$right2_s.$right3_s;
//3.输入跳转:
$inputNav="<input type='text' id='tzpage' value='".$pageIndex."' onkeydown=\"javascript:if(event.keyCode==13){document.getElementById('tzbtn').focus();}\"/> <input type='button' id='tzbtn' value='跳转' onclick=\"javascript:window.location.href='".$theurl."&pageIndex='+document.getElementById('tzpage').value;\"/>";
//
$outStr="<span><a href='".$firstUrl."'>首页</a></span>|<span><a href='".$previousUrl."'>上一页</a></span> ".$digitNav." <span><a href='".$nextUrl."'>下一页</a></span>|<span><a href='".$lastUrl."'>末页</a></span> ".$inputNav;
return $outStr;
}
if($resultArr){
foreach($resultArr as $key=>$value){
echo "<p>".$value['Username']."</p>";
}
echo "<br/>",($multi);
}else{
echo("没有数据");
}
?>