当前位置: 代码迷 >> PHP >> 关于分页及URL的请问
  详细解决方案

关于分页及URL的请问

热度:94   发布时间:2016-04-29 01:04:39.0
关于分页及URL的请教
现在遇到的一个关于分页传值的问题,sql语句做了限制条件 $sql="select * from record where searchtime between '$date1' and '$date2'";
当提交这个表单后,刚搜索时结果是正确的,但是进行翻页时就会变为 $sql="select * from record"的第二页

通过在网上查询,得知需要做url传值? 这个不是很明白,能不能在我的这个源码下 详细讲下这方面的知识啊?

源代码如下:

PHP code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>搜索页面</title></head><body><form name="form2" method="post" action="search.php"><div align="center"><table width="1230" height="103" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <!-- 将要列出的内容--><th width="4" height="38" bgcolor="#E3E3E3" scope="col">登记</th> <th width="30" bgcolor="#E3E3E3" scope="col">NO.</th> <th width="80" bgcolor="#E3E3E3" scope="col">用户号码</th><th width="80" bgcolor="#E3E3E3" scope="col">地址</th><th width="80" bgcolor="#E3E3E3" scope="col">行动</th><th width="80" bgcolor="#E3E3E3" scope="col">到达时间</th><th width="80" bgcolor="#E3E3E3" scope="col">出发时间</th><th width="80" bgcolor="#E3E3E3" scope="col">距离</th><th width="80" bgcolor="#E3E3E3" scope="col">邮件</th><th width="80" bgcolor="#E3E3E3" scope="col">接到邮件用时</th><th width="80" bgcolor="#E3E3E3" scope="col">误发邮件</th><th width="80" bgcolor="#E3E3E3" scope="col">发信邮箱</th><th width="80" bgcolor="#E3E3E3" scope="col">机器</th></tr> <?php$link=mysql_connect('localhost','root','mysql');  //数据库用户名及数据库密码mysql_select_db('BERecord');  //数据库名称mysql_query('set names utf8');$Page_size=10;  //设置每页显示个数if(!empty($date1) || !empty($date2)){$result=mysql_query("select * from record where searchtime between '$date1' and '$date2'");   }//record为表名else if(!empty($date1)){$result=mysql_query("select * from record where searchtime > '$date1'");}else if(!empty($date2)){$result=mysql_query("select * from record where searchtime < '$date2'");}else {$result=mysql_query("select * from record");}$count = mysql_num_rows($result);$page_count = ceil($count/$Page_size);$init = 1;$page_len = 7;$max_p=$page_count;$pages=$page_count;//判断当前页码if(empty($_GET['page'])||$_GET['page']<0){$page=1;}else{$page=$_GET['page'];}$offset=$Page_size*($page-1);//if(!$date1 || !$date2)if($date1 != "" && $date2 != ""){$sql="select * from record where searchtime between '$date1' and '$date2' limit $offset,$Page_size";}else if($date1 != ""  ){$sql="select * from record where searchtime > '$date1'";}else if($date2 != ""){$sql="select * from record where searchtime < '$date2'";}else{$sql="select * from record limit $offset,$Page_size";}$result=mysql_query($sql,$link);//$row=mysql_fetch_array($result);       while($row=mysql_fetch_array($result)){?><tr> <td bgcolor="#E0EEE0" height="25px"><div align="center"> <?php echo $row['record'] ?></div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['no']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['usercode']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['buildname']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['zhixing']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['arrivetime']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['gotime']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['distance']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['mail']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['receivetime']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['mistake']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['send']?> </div></td><td bgcolor="#E0EEE"><div align="center"> <?php echo $row['machine']?> </div></td></tr><?php}$page_len=($page_len%2)?$page_len:$pagelen+1;//页码个数$pageoffset = ($page_len-1)/2; //页码个数左右偏移量$key='<div class="page">';$key.="<span>$page/$pages</span>"; //第几页,共几页if($page!=1){$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=1\">第一页</a>";//第一页$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\">上一页</a>"; //上一页}else{$key.="第一页";//第一页$key.="上一页";//上一页}if($pages>$page_len){//如果当前页小于等于左偏移if($page<=$pageoffset){$init=1;$max_p = $page_len;}else{//如果当前页大于左偏移//如果当前页码右偏移超出最大分页数if($page+$pageoffset>=$pages+1){$init = $pages-$page_len+1;}else{//左右偏移都存在时的计算$init = $page-$pageoffset;$max_p =$page+$pageoffset;}}}for($i=$init;$i<=$max_p;$i++){if($i==$page){$key.='<span>'.$i.'</span>';}else{$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".$i."\">".$i."</a>";}}if($page!=$pages){$key.="<a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\">下一页</a>"; //下一页$key.="<a href=\"".$_SERVER['PHP_SELF']."?page={$pages}\">最后一页</a>"; //最后一页}else{$key.="下一页"; //下一页$key.="最后一页";//最后一页}$key.='</div>';?><tr> <td colspan="13" bgcolor="#E0EEE0"><div align="center"><?php echo $key ?></div></td> </tr> </table> </div><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></table> <input type="text" name="date1" />到<input type="text" name="date2" />  <input type="submit" value="查询" /><?phpecho $date1;echo $date2;?><div align="center"><input type="button" value="返回首页" onClick="location.href='index.php'" /></div></form>
  相关解决方案