效果:
conn.PHP 代码如下:
PHP
$conn = @ MysqL_connect("localhost","root","") or die("数据库链接错误");
MysqL_select_db("form",$conn);
MysqL_query("set names 'GBK'"); //使用GBK中文编码;
//替换空格,回车键
function htmtocode($content) {
$content = str_replace("\n","
",str_replace(" ","",$content));
return $content;
}
?>
$conn = @ MysqL_connect("localhost","root","") or die("数据库链接错误");
MysqL_select_db("form",$conn);
MysqL_query("set names 'GBK'"); //使用GBK中文编码;
//替换空格,回车键
function htmtocode($content) {
$content = str_replace("\n","
",str_replace(" ","",$content));
return $content;
}
?>
page.PHP
代码如下:
1 PHP
2
3 function _PAGEFT($totle,$displaypg = 20,$url = '') {
4
5 global $page,$firstcount,$pagenav,$_SERVER;
6
7 $GLOBALS["displaypg"] = $displaypg;
8
9 if (!$page)
$page = 1;
if (!$url) {
$url = $_SERVER["REQUEST_URI"];
}
//URL分析:
$parse_url = parse_url($url);
$url_query = $parse_url["query"]; //单独取出URL的查询字串
if ($url_query) {
$url_query = ereg_replace("(^|&)page=$page",$url_query);
$url = str_replace($parse_url["query"],$url_query,$url);
if ($url_query)
$url .= "&page";
else
$url .= "page";
} else {
$url .= "?page";
}
$lastpg = ceil($totle / $displaypg); //最后页,也是总页数
$page = min($lastpg,$page);
$prepg = $page -1; //上一页
$nextpg = ($page == $lastpg ? 0 : $page +1); //下一页
$firstcount = ($page -1) * $displaypg;
//开始分页导航条代码:
$pagenav = "显示第 " . ($totle ? ($firstcount +1) : 0) . "-" . min($firstcount + $displaypg,$totle) . " 条记录,共 $totle 条记录";
//如果只有一页则跳出函数:
if ($lastpg <= 1)
return false;
$pagenav .= " 首页 ";
if ($prepg)
$pagenav .= " 前页 ";
else
$pagenav .= " 前页 ";
if ($nextpg)
$pagenav .= " 后页 ";
else
$pagenav .= " 后页 ";
$pagenav .= " 尾页 ";
//下拉跳转列表,循环列出所有页码:
$pagenav .= " 到第 页,共 $lastpg 页";
}
include("conn.PHP");
$result=MysqL_query("SELECT * FROM `test`");
$total=MysqL_num_rows($result);
//调用pageft(),每页显示10条信息(使用默认的20时,可以省略此参数),使用本页URL(默认,所以省略掉)。
_PAGEFT($total,5);
echo $pagenav;
$result=MysqL_query("SELECT * FROM `test` limit $firstcount,$displaypg ");
while($row=MysqL_fetch_array($result)){
echo "
".$row[name]." | ".$row[sex];
}
?>
2
3 function _PAGEFT($totle,$displaypg = 20,$url = '') {
4
5 global $page,$firstcount,$pagenav,$_SERVER;
6
7 $GLOBALS["displaypg"] = $displaypg;
8
9 if (!$page)
$page = 1;
if (!$url) {
$url = $_SERVER["REQUEST_URI"];
}
//URL分析:
$parse_url = parse_url($url);
$url_query = $parse_url["query"]; //单独取出URL的查询字串
if ($url_query) {
$url_query = ereg_replace("(^|&)page=$page",$url_query);
$url = str_replace($parse_url["query"],$url_query,$url);
if ($url_query)
$url .= "&page";
else
$url .= "page";
} else {
$url .= "?page";
}
$lastpg = ceil($totle / $displaypg); //最后页,也是总页数
$page = min($lastpg,$page);
$prepg = $page -1; //上一页
$nextpg = ($page == $lastpg ? 0 : $page +1); //下一页
$firstcount = ($page -1) * $displaypg;
//开始分页导航条代码:
$pagenav = "显示第 " . ($totle ? ($firstcount +1) : 0) . "-" . min($firstcount + $displaypg,$totle) . " 条记录,共 $totle 条记录";
//如果只有一页则跳出函数:
if ($lastpg <= 1)
return false;
$pagenav .= " 首页 ";
if ($prepg)
$pagenav .= " 前页 ";
else
$pagenav .= " 前页 ";
if ($nextpg)
$pagenav .= " 后页 ";
else
$pagenav .= " 后页 ";
$pagenav .= " 尾页 ";
//下拉跳转列表,循环列出所有页码:
$pagenav .= " 到第 页,共 $lastpg 页";
}
include("conn.PHP");
$result=MysqL_query("SELECT * FROM `test`");
$total=MysqL_num_rows($result);
//调用pageft(),每页显示10条信息(使用默认的20时,可以省略此参数),使用本页URL(默认,所以省略掉)。
_PAGEFT($total,5);
echo $pagenav;
$result=MysqL_query("SELECT * FROM `test` limit $firstcount,$displaypg ");
while($row=MysqL_fetch_array($result)){
echo "
".$row[name]." | ".$row[sex];
}
?>
list.PHP
代码如下:
PHP
include("conn.PHP");
$pagesize=5;
$url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
$numq=MysqL_query("SELECT * FROM `test`");
$num = MysqL_num_rows($numq);
if($_GET){
$pageval=$_GET;
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval<=1)$pageval=1;
echo "共 $num 条".
" ";
}
$sql="SELECT * FROM `test` limit $page $pagesize ";
$query=MysqL_query($sql);
while($row=MysqL_fetch_array($query)){
echo "
".$row[name]." | ".$row[sex];
}
?>
include("conn.PHP");
$pagesize=5;
$url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
$numq=MysqL_query("SELECT * FROM `test`");
$num = MysqL_num_rows($numq);
if($_GET){
$pageval=$_GET;
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval<=1)$pageval=1;
echo "共 $num 条".
" ";
}
$sql="SELECT * FROM `test` limit $page $pagesize ";
$query=MysqL_query($sql);
while($row=MysqL_fetch_array($query)){
echo "
".$row[name]." | ".$row[sex];
}
?>
分页公式:(当前页数-1)*每页条数,每页条数
sql语句:select * from test_table limit ($page-1)*$pageSize,$pageSize;