[十万火急]分页问题,每当我搜索完后连接到第二页时会出错是什么问题
时间:2008-06-20
来源:互联网
复制内容到剪贴板
<?php
/////////$pagerecords为每页所显示的记录数,$totalrecords为总的记录数/////////
function showPageNav($pagerecords,$totalrecords)
{
$page=$_GET["page"];
if(!$page)$page=1;
///////分析URL//////////////////////
if(!$url)
{ $url=$_SERVER["REQUEST_URI"];}
$parse_url=parse_url($url);
$url_query=$parse_url["query"];
///////替换地址后面的page参数//////////
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";
}
///////////////////计算页码///////////////////
$totalpages=ceil($totalrecords/$pagerecords);
$lastpg=$totalpages;
$page=min($totalpages,$page);
$prepg=$page-1;
$nextpg=($page==$totalpages?0:$page+1);
//////////////////编写分页导航代码///////////
$firstcount=($page-1)*$pagerecords;
//$showresult="显示第 ".($totalrecords?($firstcount+1):0)." - ".min($firstcount+$pagerecords,$totalrecords)." 条记录,共 $totalrecords 条记录 ";
//////////////如果只有一个页,不显示分页导航////////////
if($lastpg<=1)
{
return "";
}
$showresult.="<a href='$url=1'>首页</a>";
if($prepg)
{$showresult.=" <a href='$url=$prepg'>前页</a>";}
else
{$showresult.=" 前页";}
if($nextpg)
{$showresult.=" <a href='$url=$nextpg'>后页</a>";}
else
{ $showresult.=" 后页";}
$showresult.=" <a href='$url=$lastpg'>尾页</a>";
$showresult.=" 跳转到第<select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for($i=1;$i<=$lastpg;$i++)
{
if($i==$page) $showresult.="<option value='$i' selected>$i</option>\n";
else $showresult.="<option value='$i'>$i</option>\n";
}
$showresult.="</select> 页,共 $lastpg 页";
echo($showresult);
}
?>
search.php文件
<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
.TIAN {font-size: 12px;
color: #000000;
}
.ty1 { border-bottom-width: thin;
border-bottom-style: solid;
border-bottom-color: #868FDC;
}
-->
</style>
<style type="text/css">
a:link {
color: #000000;
text-decoration: none;
}
.y1 {
border-bottom-width: thin;
border-bottom-style: solid;
border-bottom-color: #0000FF;
}
a:visited {
text-decoration: none;
color: #000000;
}
a:hover {
text-decoration: none;
color: #000000;
}
a:active {
text-decoration: none;
color: #000000;
}
a {
font-family: 宋体;
font-size: 12px;
}
body,td,th {
font-family: 宋体;
font-size: 12px;
}
.STYLE3 {color: #FFFFFF;
font-weight: bold;
}
.STYLE4 {color: #FF0000}
</style>
</head>
<body>
<?php
include('header.php');
include('web_top1.php');
include('./fun/fun.php');
require_once("./fun/page.php");
$GPID = $_GET['Gcategory'];
//if(empty($GPID))
//{
//alert_back('非法操作!');
//}
include('fun/dbconnect.php');
//$select_str = "select GPname from gproducts where GPID='$GPID'";
//$result_str = mysql_query($select_str);
//list($GPname)=mysql_fetch_row($result_str);
$spid = $_POST['Scategory'];
$key = $_POST['key'];
$frompice = $_POST['fromprice'];
$topprice = $_POST['topprice'];
//if(empty($spid))
//{
// $spid = $arr_str['SPID'];
//}
///////$recordcount定义每一页所要显示的记录数////////////////
$pagerecords=6;
////////读取page当前页号/////////////////////////
$page=$_GET["page"];
if($page=="")
{
$page=1;
}
///////计算当前页号所对应的记录的开始处$start/////////
$start=$pagerecords*($page-1);
///////编写查询当前页号的记录///////////////////////////
$selectSearchSQL = "select PID,Pname,Pno,Para,Price,Photo,Pstock,Pfact,Pack,Plotno,Pspeci from products where PSPID='$spid' and";
////////编写获取总记录数的SQL语句//////////////////////
$selectCountSQL = "select count(*) from products where PSPID='$spid' and";
if($key!='' && $fromprice!='' && $topprice!='')
{
$selectSearchSQL.=" Pname like '%$key%' and Price>=$fromprice and Price<=$topprice";
$selectCountSQL.=" Pname like '%$key%' and Price>=$fromprice and Price<=$topprice";
}
if($key!="" && $fromprice!="" && $topprice=="")
{
$selectSearchSQL.=" Pname like '%$key%' and Price>=$fromprice";
$selectCountSQL.=" Pname like '%$key%' and Pprice>=$fromprice ";
}
if($key!="" && $fromprice=="" && $topprice!="")
{
$selectSearchSQL.=" Pname like '%$key%' and Price<=$topprice";
$selectCountSQL.=" Pname like '%$key%' and Price<=$topprice";
}
if(!empty($key) && $fromprice=="" && empty($topprice))
{
$selectSearchSQL.=" Pname like '%$key%'";
$selectCountSQL.=" Pname like '%$key%'";
}
if($key=="" && $fromprice!="" && $topprice!="")
{
$selectSearchSQL.=" Price>=$fromprice and Price<=$topprice";
$selectCountSQL.=" Price>=$fromprice and Price<=$topprice";
}
if($key=="" && $fromprice=="" && $topprice!="")
{
$selectSearchSQL.=" Price<=$topprice";
$selectCountSQL.=" Price<=$topprice";
}
if($key=="" && $fromprice!="" && $topprice=="")
{
$selectSearchSQL.=" Price>=$fromprice";
$selectCountSQL.=" Price>=$fromprice";
}
$selectSearchSQL .= " order by Pdate desc limit $start,$pagerecords";
$selectCountSQL .= " order by Pdate desc ";
echo $selectSearchSQL.'<br/>';
echo $selectCountSQL.'<br/>';
$result_sprod = mysql_query($selectSearchSQL);
$resultCount=mysql_query($selectCountSQL);
list($totalrecords)=mysql_fetch_row($resultCount);
?>
<table width="800" height="32" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30" bgcolor="#FFFFFF" class="y1"> 您当前的位置:<a href="index.php">赛格电子市场高科德交易中心</a> >> <?php echo '您搜索<font color="red">"'.$key.'"</font>结果'; ?></td>
</tr>
</table>
<?php
$result=mysql_fetch_array($result_sprod);
if($result!=''){
?>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="426" height="28" valign="middle" background="images/xingc1.gif"> </td>
<td width="374" valign="middle" background="images/xingc2.gif"><div align="right">
<?php showPageNav($pagerecords,$totalrecords);?>
</div></td>
</tr>
<tr>
<td height="104" colspan="2">
<table width="800" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="100" height="20" align="left" valign="bottom" background="images/xing.gif"><div align="center" class="STYLE3">图片</div></td>
<td width="299" height="20" align="left" valign="bottom" background="images/xing1.gif"><div align="center"><span class="STYLE3"> 产品信息</span></div></td>
<td width="83" height="20" align="left" valign="bottom" background="images/xing2.gif"><div align="center" class="STYLE3">库存 </div></td>
<td width="176" height="20" align="left" valign="bottom" background="images/xing31.gif"><div align="center" class="STYLE3">公司及联系方式</div></td>
<td width="82" height="20" align="left" valign="bottom" background="images/xing2.gif"><div align="center" class="STYLE3">单价 </div></td>
<td width="60" height="20" align="left" valign="bottom" background="images/xingb1.gif"><div align="center" class="STYLE3">留言</div></td>
</tr>
</table>
<table width="800" height="100" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<?php
while($arr_sprod = mysql_fetch_array($result_sprod))
{
?>
<tr>
<td width="96" height="79" align="left" background="images/xingd.gif" bgcolor="#FFFFFF"><div align="center"><img name="" src="<?php echo $arr_sprod['Photo']; ?>" width="92" height="79" alt="" /></div></td>
<td width="300" align="left" background="Web_Images/xingb.gif" bgcolor="#FFFFFF">
<?php if($arr_sprod['Pname'] != '') echo '产品名称:'.$arr_sprod['Pname'].'<br/>'; ?>
<?php if($arr_sprod['Pno'] != '') echo '产品型号:'. $arr_sprod['Pno'].'<br/>'; ?>
<?php if($arr_sprod['Pspeci'] != '') echo '规格:'.$arr_sprod['Pspeci'].'<br/>'; ?>
<?php if($arr_sprod['Para'] != '') echo '参数:'.$arr_sprod['Para']."<br/>"; ?>
<?php if($arr_sprod['Pack'] != '') echo '封装:'.$arr_sprod['Pack']."<br/>"; ?>
<?php if($arr_sprod['Plotno'] != '') echo '批号:'.$arr_sprod['Plotno']."<br/>"; ?>
<?php if($arr_sprod['Pfact'] != '') echo '生产商:'.$arr_sprod['Pfact']; ?></td>
<td width="84" align="center" background="Web_Images/xingc.gif" bgcolor="#FFFFFF"style="WORD-BREAK:break-all">
<?php echo $arr_sprod['Pstock']; ?></td>
<td width="174" align="left" background="Web_Images/xingA.gif" bgcolor="#FFFFFF"> </td>
<td width="80" align="center" background="Web_Images/xingc.gif" bgcolor="#FFFFFF"style="WORD-BREAK:break-all"><span class="STYLE4"><?php if($arr_sprod['Price']!='')
{
echo $arr_sprod['Price'];
}else{
echo '价格面议';
}
?></span></td>
<td width="59" align="left" background="Web_Images/xingA1.gif" bgcolor="#FFFFFF"><div align="center"><a href="gongshi12.asp?ID="><img src="images/liuyan.jpg" width="49" height="26" border="0"/></a></div></td>
</tr>
<?php }
?>
</table></td>
</tr>
<tr>
<td width="426" height="28" valign="middle" background="images/xingd1.gif"> </td>
<td height="28" valign="middle" background="images/xingd2.gif"><div align="right">
<?php showPageNav($pagerecords,$totalrecords);?></div></td>
</tr>
</table>
<?php
}else{
echo"<div align='center'><font color='red'>非常抱歉!无该商品记录!</font></div>";
}
include('footer.php');
?>
</body>
</html>
代码:
page.php文件<?php
/////////$pagerecords为每页所显示的记录数,$totalrecords为总的记录数/////////
function showPageNav($pagerecords,$totalrecords)
{
$page=$_GET["page"];
if(!$page)$page=1;
///////分析URL//////////////////////
if(!$url)
{ $url=$_SERVER["REQUEST_URI"];}
$parse_url=parse_url($url);
$url_query=$parse_url["query"];
///////替换地址后面的page参数//////////
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";
}
///////////////////计算页码///////////////////
$totalpages=ceil($totalrecords/$pagerecords);
$lastpg=$totalpages;
$page=min($totalpages,$page);
$prepg=$page-1;
$nextpg=($page==$totalpages?0:$page+1);
//////////////////编写分页导航代码///////////
$firstcount=($page-1)*$pagerecords;
//$showresult="显示第 ".($totalrecords?($firstcount+1):0)." - ".min($firstcount+$pagerecords,$totalrecords)." 条记录,共 $totalrecords 条记录 ";
//////////////如果只有一个页,不显示分页导航////////////
if($lastpg<=1)
{
return "";
}
$showresult.="<a href='$url=1'>首页</a>";
if($prepg)
{$showresult.=" <a href='$url=$prepg'>前页</a>";}
else
{$showresult.=" 前页";}
if($nextpg)
{$showresult.=" <a href='$url=$nextpg'>后页</a>";}
else
{ $showresult.=" 后页";}
$showresult.=" <a href='$url=$lastpg'>尾页</a>";
$showresult.=" 跳转到第<select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for($i=1;$i<=$lastpg;$i++)
{
if($i==$page) $showresult.="<option value='$i' selected>$i</option>\n";
else $showresult.="<option value='$i'>$i</option>\n";
}
$showresult.="</select> 页,共 $lastpg 页";
echo($showresult);
}
?>
search.php文件
<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
.TIAN {font-size: 12px;
color: #000000;
}
.ty1 { border-bottom-width: thin;
border-bottom-style: solid;
border-bottom-color: #868FDC;
}
-->
</style>
<style type="text/css">
a:link {
color: #000000;
text-decoration: none;
}
.y1 {
border-bottom-width: thin;
border-bottom-style: solid;
border-bottom-color: #0000FF;
}
a:visited {
text-decoration: none;
color: #000000;
}
a:hover {
text-decoration: none;
color: #000000;
}
a:active {
text-decoration: none;
color: #000000;
}
a {
font-family: 宋体;
font-size: 12px;
}
body,td,th {
font-family: 宋体;
font-size: 12px;
}
.STYLE3 {color: #FFFFFF;
font-weight: bold;
}
.STYLE4 {color: #FF0000}
</style>
</head>
<body>
<?php
include('header.php');
include('web_top1.php');
include('./fun/fun.php');
require_once("./fun/page.php");
$GPID = $_GET['Gcategory'];
//if(empty($GPID))
//{
//alert_back('非法操作!');
//}
include('fun/dbconnect.php');
//$select_str = "select GPname from gproducts where GPID='$GPID'";
//$result_str = mysql_query($select_str);
//list($GPname)=mysql_fetch_row($result_str);
$spid = $_POST['Scategory'];
$key = $_POST['key'];
$frompice = $_POST['fromprice'];
$topprice = $_POST['topprice'];
//if(empty($spid))
//{
// $spid = $arr_str['SPID'];
//}
///////$recordcount定义每一页所要显示的记录数////////////////
$pagerecords=6;
////////读取page当前页号/////////////////////////
$page=$_GET["page"];
if($page=="")
{
$page=1;
}
///////计算当前页号所对应的记录的开始处$start/////////
$start=$pagerecords*($page-1);
///////编写查询当前页号的记录///////////////////////////
$selectSearchSQL = "select PID,Pname,Pno,Para,Price,Photo,Pstock,Pfact,Pack,Plotno,Pspeci from products where PSPID='$spid' and";
////////编写获取总记录数的SQL语句//////////////////////
$selectCountSQL = "select count(*) from products where PSPID='$spid' and";
if($key!='' && $fromprice!='' && $topprice!='')
{
$selectSearchSQL.=" Pname like '%$key%' and Price>=$fromprice and Price<=$topprice";
$selectCountSQL.=" Pname like '%$key%' and Price>=$fromprice and Price<=$topprice";
}
if($key!="" && $fromprice!="" && $topprice=="")
{
$selectSearchSQL.=" Pname like '%$key%' and Price>=$fromprice";
$selectCountSQL.=" Pname like '%$key%' and Pprice>=$fromprice ";
}
if($key!="" && $fromprice=="" && $topprice!="")
{
$selectSearchSQL.=" Pname like '%$key%' and Price<=$topprice";
$selectCountSQL.=" Pname like '%$key%' and Price<=$topprice";
}
if(!empty($key) && $fromprice=="" && empty($topprice))
{
$selectSearchSQL.=" Pname like '%$key%'";
$selectCountSQL.=" Pname like '%$key%'";
}
if($key=="" && $fromprice!="" && $topprice!="")
{
$selectSearchSQL.=" Price>=$fromprice and Price<=$topprice";
$selectCountSQL.=" Price>=$fromprice and Price<=$topprice";
}
if($key=="" && $fromprice=="" && $topprice!="")
{
$selectSearchSQL.=" Price<=$topprice";
$selectCountSQL.=" Price<=$topprice";
}
if($key=="" && $fromprice!="" && $topprice=="")
{
$selectSearchSQL.=" Price>=$fromprice";
$selectCountSQL.=" Price>=$fromprice";
}
$selectSearchSQL .= " order by Pdate desc limit $start,$pagerecords";
$selectCountSQL .= " order by Pdate desc ";
echo $selectSearchSQL.'<br/>';
echo $selectCountSQL.'<br/>';
$result_sprod = mysql_query($selectSearchSQL);
$resultCount=mysql_query($selectCountSQL);
list($totalrecords)=mysql_fetch_row($resultCount);
?>
<table width="800" height="32" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30" bgcolor="#FFFFFF" class="y1"> 您当前的位置:<a href="index.php">赛格电子市场高科德交易中心</a> >> <?php echo '您搜索<font color="red">"'.$key.'"</font>结果'; ?></td>
</tr>
</table>
<?php
$result=mysql_fetch_array($result_sprod);
if($result!=''){
?>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="426" height="28" valign="middle" background="images/xingc1.gif"> </td>
<td width="374" valign="middle" background="images/xingc2.gif"><div align="right">
<?php showPageNav($pagerecords,$totalrecords);?>
</div></td>
</tr>
<tr>
<td height="104" colspan="2">
<table width="800" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="100" height="20" align="left" valign="bottom" background="images/xing.gif"><div align="center" class="STYLE3">图片</div></td>
<td width="299" height="20" align="left" valign="bottom" background="images/xing1.gif"><div align="center"><span class="STYLE3"> 产品信息</span></div></td>
<td width="83" height="20" align="left" valign="bottom" background="images/xing2.gif"><div align="center" class="STYLE3">库存 </div></td>
<td width="176" height="20" align="left" valign="bottom" background="images/xing31.gif"><div align="center" class="STYLE3">公司及联系方式</div></td>
<td width="82" height="20" align="left" valign="bottom" background="images/xing2.gif"><div align="center" class="STYLE3">单价 </div></td>
<td width="60" height="20" align="left" valign="bottom" background="images/xingb1.gif"><div align="center" class="STYLE3">留言</div></td>
</tr>
</table>
<table width="800" height="100" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<?php
while($arr_sprod = mysql_fetch_array($result_sprod))
{
?>
<tr>
<td width="96" height="79" align="left" background="images/xingd.gif" bgcolor="#FFFFFF"><div align="center"><img name="" src="<?php echo $arr_sprod['Photo']; ?>" width="92" height="79" alt="" /></div></td>
<td width="300" align="left" background="Web_Images/xingb.gif" bgcolor="#FFFFFF">
<?php if($arr_sprod['Pname'] != '') echo '产品名称:'.$arr_sprod['Pname'].'<br/>'; ?>
<?php if($arr_sprod['Pno'] != '') echo '产品型号:'. $arr_sprod['Pno'].'<br/>'; ?>
<?php if($arr_sprod['Pspeci'] != '') echo '规格:'.$arr_sprod['Pspeci'].'<br/>'; ?>
<?php if($arr_sprod['Para'] != '') echo '参数:'.$arr_sprod['Para']."<br/>"; ?>
<?php if($arr_sprod['Pack'] != '') echo '封装:'.$arr_sprod['Pack']."<br/>"; ?>
<?php if($arr_sprod['Plotno'] != '') echo '批号:'.$arr_sprod['Plotno']."<br/>"; ?>
<?php if($arr_sprod['Pfact'] != '') echo '生产商:'.$arr_sprod['Pfact']; ?></td>
<td width="84" align="center" background="Web_Images/xingc.gif" bgcolor="#FFFFFF"style="WORD-BREAK:break-all">
<?php echo $arr_sprod['Pstock']; ?></td>
<td width="174" align="left" background="Web_Images/xingA.gif" bgcolor="#FFFFFF"> </td>
<td width="80" align="center" background="Web_Images/xingc.gif" bgcolor="#FFFFFF"style="WORD-BREAK:break-all"><span class="STYLE4"><?php if($arr_sprod['Price']!='')
{
echo $arr_sprod['Price'];
}else{
echo '价格面议';
}
?></span></td>
<td width="59" align="left" background="Web_Images/xingA1.gif" bgcolor="#FFFFFF"><div align="center"><a href="gongshi12.asp?ID="><img src="images/liuyan.jpg" width="49" height="26" border="0"/></a></div></td>
</tr>
<?php }
?>
</table></td>
</tr>
<tr>
<td width="426" height="28" valign="middle" background="images/xingd1.gif"> </td>
<td height="28" valign="middle" background="images/xingd2.gif"><div align="right">
<?php showPageNav($pagerecords,$totalrecords);?></div></td>
</tr>
</table>
<?php
}else{
echo"<div align='center'><font color='red'>非常抱歉!无该商品记录!</font></div>";
}
include('footer.php');
?>
</body>
</html>
作者: hxmtrend 发布时间: 2008-06-20
很简单,链接搜索后的页码丢失了搜索条件的参数。
以后不要罗列这么多代码,没人会认真去看,把问题简单说清楚就可以了。
以后不要罗列这么多代码,没人会认真去看,把问题简单说清楚就可以了。
作者: majiguang 发布时间: 2008-06-20


作者: luzhou 发布时间: 2008-06-21
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28