PHP怎么制作分页
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>询价系统</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {
}
</style>
</head>
<?php
@$conn = mysql_connect('localhost','root','')
or die ("数据库打开失败");
if (mysql_select_db("xunjia",$conn))
{ echo "";}
else
{ echo"我们没有打开某某数据库";}
mysql_query("SET NAMES 'utf8'");
@ $xj1=$_POST["changjia"];
@ $xj2=$_POST["mingcheng"];
@ $xj3=$_POST["xinghao"];
if ($xj1=="" and $xj2=="" and $xj3=="")
{
$rs=mysql_query("select * from bjlb",$conn)
or die("失败");
}
elseif($xj1!="" and $xj2=="" and $xj3=="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE changjia='$xj1'")
or die("失败");
}
elseif($xj1=="" and $xj2!="" and $xj3=="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE mingcheng='$xj2'")
or die("失败");
}
elseif($xj1=="" and $xj2=="" and $xj3!="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE xinghao='$xj3'")
or die("失败");
}
elseif($xj1!="" and $xj2!="" and $xj3=="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE changjia='$xj1' and mingcheng='$xj2' ")
or die("失败");
}
elseif($xj1=="" and $xj2!="" and $xj3!="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE xinghao='$xj3' and mingcheng='$xj2' ")
or die("失败");
}
elseif($xj1!="" and $xj2!="" and $xj3!="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE changjia='$xj1' and mingcheng='$xj2' and xinghao='$xj3' ")
or die("失败");
}
elseif($xj1!="" and $xj2=="" and $xj3!="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE changjia='$xj1' and xinghao='$xj3' ")
or die("失败");
}
$array=mysql_fetch_array($rs);
$rows = mysql_num_rows($rs);//取得记录数量
echo '一共查询到';echo $rows;echo '条记录'; //输出条数$rows
?>
循环输出后怎么分页 展开
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>询价系统</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {
}
</style>
</head>
<?php
@$conn = mysql_connect('localhost','root','')
or die ("数据库打开失败");
if (mysql_select_db("xunjia",$conn))
{ echo "";}
else
{ echo"我们没有打开某某数据库";}
mysql_query("SET NAMES 'utf8'");
@ $xj1=$_POST["changjia"];
@ $xj2=$_POST["mingcheng"];
@ $xj3=$_POST["xinghao"];
if ($xj1=="" and $xj2=="" and $xj3=="")
{
$rs=mysql_query("select * from bjlb",$conn)
or die("失败");
}
elseif($xj1!="" and $xj2=="" and $xj3=="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE changjia='$xj1'")
or die("失败");
}
elseif($xj1=="" and $xj2!="" and $xj3=="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE mingcheng='$xj2'")
or die("失败");
}
elseif($xj1=="" and $xj2=="" and $xj3!="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE xinghao='$xj3'")
or die("失败");
}
elseif($xj1!="" and $xj2!="" and $xj3=="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE changjia='$xj1' and mingcheng='$xj2' ")
or die("失败");
}
elseif($xj1=="" and $xj2!="" and $xj3!="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE xinghao='$xj3' and mingcheng='$xj2' ")
or die("失败");
}
elseif($xj1!="" and $xj2!="" and $xj3!="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE changjia='$xj1' and mingcheng='$xj2' and xinghao='$xj3' ")
or die("失败");
}
elseif($xj1!="" and $xj2=="" and $xj3!="")
{
$rs=mysql_query( "SELECT id,changjia,fenlei,bianhao,mingcheng,xinghao,danjia,guige,beizhu FROM bjlb WHERE changjia='$xj1' and xinghao='$xj3' ")
or die("失败");
}
$array=mysql_fetch_array($rs);
$rows = mysql_num_rows($rs);//取得记录数量
echo '一共查询到';echo $rows;echo '条记录'; //输出条数$rows
?>
循环输出后怎么分页 展开
2个回答
2016-07-08 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
方法/步骤
首先要了解下分页的原理即
SELECT * FROM table …… limit 开始位置 , 操作条数
开始位置从0开始
SELECT * FROM table …… limit 0 , 20
取最前面20条
SELECT * FROM table …… limit 10 , 20
11条到20条
其次分页要用的公式
得到公式
(当前页数 - 1 )X 每页条数 , 每页条数
Select * from table limit ($Page- 1) * $PageSize, $PageSize
还要了解parse_url()解析URL函数
parse_url() 是讲URL解析成有固定键值的数组的函数
$ua=parse_url("http://username:password@hostname/path?arg=value#anchor");
print_r($ua);
结果:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
创建数据库bbs和表test有
CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) character set gb2312 NOT NULL,
`sex` varchar(2) character set gb2312 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
然后插入十几条测试数据即可
写mysql数据库连接代码保存conn.php文件里
代码如下
<?php
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
function htmtocode($content) {
$content = str_replace("\n", "<br>", str_replace(" ", " ", $content));
return $content;
}
//$content=str_replace("'","'",$content);
//htmlspecialchars();
?>
写分页函数page.php
代码如下
<?php
function _PAGEFT($totle, $displaypg = 20, $url = '') {
global $page, $firstcount, $pagenav, $_SERVER;
$GLOBALS["displaypg"] = $displaypg;
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 = "显示第 <B>" . ($totle ? ($firstcount +1) : 0) . "</B>-<B>" . min($firstcount + $displaypg, $totle) . "</B> 条记录,共 $totle 条记录";
//如果只有一页则跳出函数:
if ($lastpg <= 1)
return false;
$pagenav .= " <a href='$url=1'>首页</a> ";
if ($prepg)
$pagenav .= " <a href='$url=$prepg'>前页</a> ";
else
$pagenav .= " 前页 ";
if ($nextpg)
$pagenav .= " <a href='$url=$nextpg'>后页</a> ";
else
$pagenav .= " 后页 ";
$pagenav .= " <a href='$url=$lastpg'>尾页</a> ";
//下拉跳转列表,循环列出所有页码:
$pagenav .= " 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for ($i = 1; $i <= $lastpg; $i++) {
if ($i == $page)
$pagenav .= "<option value='$i' selected>$i</option>\n";
else
$pagenav .= "<option value='$i'>$i</option>\n";
}
$pagenav .= "</select> 页,共 $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 "<hr><b>".$row[name]." | ".$row[sex];
}
?>
调用数据和分页list.php
<?php
/**
* 爱微网 www.iiwnet.com/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[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval<=1)$pageval=1;
echo "共 $num 条".
" <a href=$url?page=".($pageval-1).">上一页</a> <a href=$url?page=".($pageval+1).">下一页</a>";
}
echo $SQL="SELECT * FROM `test` limit $page $pagesize ";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){
echo "<hr><b>".$row[name]." | ".$row[sex];
}
?>
注意事项
注意分页公式写法你只要记住即可;
(当前页数 - 1 )X 每页条数 , 每页条数;
Select * from table limit ($Page- 1) * $PageSize, $PageSize;
注意三个代码文件在同一个目录下;
主要是最后的list.php调用代码要细看很有用。
首先要了解下分页的原理即
SELECT * FROM table …… limit 开始位置 , 操作条数
开始位置从0开始
SELECT * FROM table …… limit 0 , 20
取最前面20条
SELECT * FROM table …… limit 10 , 20
11条到20条
其次分页要用的公式
得到公式
(当前页数 - 1 )X 每页条数 , 每页条数
Select * from table limit ($Page- 1) * $PageSize, $PageSize
还要了解parse_url()解析URL函数
parse_url() 是讲URL解析成有固定键值的数组的函数
$ua=parse_url("http://username:password@hostname/path?arg=value#anchor");
print_r($ua);
结果:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
创建数据库bbs和表test有
CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) character set gb2312 NOT NULL,
`sex` varchar(2) character set gb2312 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
然后插入十几条测试数据即可
写mysql数据库连接代码保存conn.php文件里
代码如下
<?php
$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
function htmtocode($content) {
$content = str_replace("\n", "<br>", str_replace(" ", " ", $content));
return $content;
}
//$content=str_replace("'","'",$content);
//htmlspecialchars();
?>
写分页函数page.php
代码如下
<?php
function _PAGEFT($totle, $displaypg = 20, $url = '') {
global $page, $firstcount, $pagenav, $_SERVER;
$GLOBALS["displaypg"] = $displaypg;
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 = "显示第 <B>" . ($totle ? ($firstcount +1) : 0) . "</B>-<B>" . min($firstcount + $displaypg, $totle) . "</B> 条记录,共 $totle 条记录";
//如果只有一页则跳出函数:
if ($lastpg <= 1)
return false;
$pagenav .= " <a href='$url=1'>首页</a> ";
if ($prepg)
$pagenav .= " <a href='$url=$prepg'>前页</a> ";
else
$pagenav .= " 前页 ";
if ($nextpg)
$pagenav .= " <a href='$url=$nextpg'>后页</a> ";
else
$pagenav .= " 后页 ";
$pagenav .= " <a href='$url=$lastpg'>尾页</a> ";
//下拉跳转列表,循环列出所有页码:
$pagenav .= " 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for ($i = 1; $i <= $lastpg; $i++) {
if ($i == $page)
$pagenav .= "<option value='$i' selected>$i</option>\n";
else
$pagenav .= "<option value='$i'>$i</option>\n";
}
$pagenav .= "</select> 页,共 $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 "<hr><b>".$row[name]." | ".$row[sex];
}
?>
调用数据和分页list.php
<?php
/**
* 爱微网 www.iiwnet.com/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[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval<=1)$pageval=1;
echo "共 $num 条".
" <a href=$url?page=".($pageval-1).">上一页</a> <a href=$url?page=".($pageval+1).">下一页</a>";
}
echo $SQL="SELECT * FROM `test` limit $page $pagesize ";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){
echo "<hr><b>".$row[name]." | ".$row[sex];
}
?>
注意事项
注意分页公式写法你只要记住即可;
(当前页数 - 1 )X 每页条数 , 每页条数;
Select * from table limit ($Page- 1) * $PageSize, $PageSize;
注意三个代码文件在同一个目录下;
主要是最后的list.php调用代码要细看很有用。
展开全部
授人以鱼不如授人以渔,我就把原理和你说一下
分页需要这几个必要参数:
1.数据总数目。
2.分页单位,就是以多少条数据为一页。
3.当前页数。
步骤如下:
假如共有$allNum = 101;条数据,以$pagesize = 10;条数据每页。那么:
1.则共有: $pages = ceil($allNum / $pagesize); 页
2.接下来定义当前页数: 获取页数$page = isset($_GET['p']) ? (int)$_GET['p'] : 1;
那么当前页数: $nowpage = (($page == 0) or ($page > $pages)) ? 1 : $page;
3.计算取开始数据的ID: $pagestart = (($nowpage-1) * $pagesize);
4.最后就使用数据库语句 ORDER BY ID desc LIMIT '{$pagestart}', '{$pagesize}'");
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询