通过关键字查询到的数据有2页,但是,当点击翻页的时候显示的并不是按关键字查询到的数据,
而是,显示查询到的全部数据。这该怎么改。
echo "<table  border=1 width=700 cellspacing=0 style=border-collapse:collapse>";
echo "<tr style=background-image:url(img/new_hader_27.jpg) height=30 style=color:#ffffff><th>选择</th><th>hotel_id</th><th>start_date</th><th>end_date</th><th>content</th><th>source</th><th>s_type</th><th>操作</th></tr>"; //查询数据
if( empty($ziduan) || empty($txt_sousuo))
    $query = "SELECT * FROM hotel_activities_2";
else
    $query = "SELECT * FROM hotel_activities_2 where $ziduan like '%{$txt_sousuo}%'";
    $result=mysql_query($query) or die("<script>alert('查询的数据不存在');window.location='chaxun_rizhi.php';</script>"); 
    $rowsPerPage=5;
    $maxRowCount=mysql_num_rows($result);
    if($maxRowCount%$rowsPerPage==0)
       {
           $maxPage=(int)($maxRowCount/$rowsPerPage);
       }
    else {
           $maxPage=(int)($maxRowCount/$rowsPerPage)+1;
          }
if (isset($_GET['curPage']))
{
    $page=$_GET['curPage'];
}
else 
    {
    $page=1;
    }
      $start=$rowsPerPage*($page-1);$sel1 = $query . " order by h_id limit $start,$rowsPerPage";$result=mysql_query($sel1) or die("查询失败");
//循环输出表中各个字段值
echo '<form action="delect_sj.php" method=post>';
while ($row = mysql_fetch_array($result)) {
    echo '<tr align="center" class="shu">';
    echo '<td><input type=checkbox name="hid[]" value="'.$row['h_id'].'"></td>';
    echo '<td>'.$row['hotel_id'].'</td>';
    echo '<td>'.$row['start_date'].'</td>';
    echo '<td>'.$row['end_date'].'</td>';
    echo '<td>'.$row['content'].'</td>';
    echo '<td>'.$row['source'].'</td>';
    echo '<td>'.$row['s_type'].'</td>';
    echo '<td>'.'&nbsp;<a href="upedit.php?hid='.$row['h_id'].'" >编辑</a>&nbsp;'.'</td>';
    echo '</tr>';
}
echo '<input type=submit value="删除所选" onclick="return  delconfirm()"></form>';
if( empty($ziduan) || empty($txt_sousuo))
    $query = "SELECT count(*) FROM hotel_activities_2";
else
    $query = "SELECT count(*) FROM hotel_activities_2 where $ziduan like '%{$txt_sousuo}%'";
$res=mysql_query("$query",$connection); 
$row = mysql_fetch_array($res); 
$numrows=$row[0]; 
$pages=intval($numrows/$rowsPerPage); 
if ($numrows%$rowsPerPage) 
$pages++; if($page>1)
{
    $prevPage=$page-1;
    echo "<a href='?curPage=$prevPage'><img src='img/prev.gif'/></a>";
}
if ($page<$maxPage)
{
    $nextPage=$page+1;
    echo "<a href='?curPage=$nextPage'><img src='img/next.gif'/></a>";
}
echo "</table>";
echo "<div align='left' class='shu'>共有".$pages."页&nbsp;当前为第".$page."页"; 

解决方案 »

  1.   

    给个完整的,你的代码太乱了。<?php
    echo "<table border=1 width=700 cellspacing=0 style=border-collapse:collapse>"; 
    echo "<tr style=background-image:url(img/new_hader_27.jpg) height=30 style=color:#ffffff><th>选择</th><th>hotel_id</th><th>start_date</th><th>end_date</th><th>content</th><th>source</th><th>s_type</th><th>操作</th></tr>"; 
    $ziduan = isset($_POST['ziduan'])?$_POST['ziduan']:(isset($_GET['ziduan'])?$_GET['ziduan']:"");
    $txt_sousuo = isset($_POST['txt_sousuo'])?$_POST['txt_sousuo']:(isset($_GET['txt_sousuo'])?$_GET['txt_sousuo']:"");$where = "where 1";
    if(!empty($ziduan) && !empty($txt_sousuo)){
    $where .= " and $ziduan like '%{$txt_sousuo}%'"; 
    }
    $sql = "SELECT count(*) as countnum FROM hotel_activities_2 $where";
    $result=mysql_fetch_array(mysql_query($sql));
    $countnum = $result['countnum'];
    if(!$countnum) die("<script>alert('查询的数据不存在');window.location='chaxun_rizhi.php';</script>");$rowsPerPage=5; 
    $maxPage=ceil($countnum/$rowsPerPage); 
    $page=isset($_GET['curPage'])?intval($_GET['curPage']):1; 
    $page = $page<1?1:$page;
    $page = $page>$maxPage?$maxPage:$page;$start=$rowsPerPage*($page-1);
    $sel1 = "SELECT * FROM hotel_activities_2 $where order by h_id limit $start,$rowsPerPage"; 
    $result=mysql_query($sel1) or die("查询失败"); 
    //循环输出表中各个字段值 
    echo '<form action="delect_sj.php" method=post>'; 
    while ($row = mysql_fetch_array($result)) { 
    echo '<tr align="center" class="shu">'; 
    echo '<td><input type=checkbox name="hid[]" value="'.$row['h_id'].'"></td>'; 
    echo '<td>'.$row['hotel_id'].'</td>'; 
    echo '<td>'.$row['start_date'].'</td>'; 
    echo '<td>'.$row['end_date'].'</td>'; 
    echo '<td>'.$row['content'].'</td>'; 
    echo '<td>'.$row['source'].'</td>'; 
    echo '<td>'.$row['s_type'].'</td>'; 
    echo '<td>'.'&nbsp;<a href="upedit.php?hid='.$row['h_id'].'" >编辑</a>&nbsp;'.'</td>'; 
    echo '</tr>'; 

    echo '<input type=submit value="删除所选" onclick="return delconfirm()"></form>'; if($page>1) { 
    $prevPage=($page-1)<1?1:$page-1;
    echo "<a href='?ziduan={$ziduan}&txt_sousuocur=".rawurlencode($txt_sousuo)."&curPage=$prevPage'><img src='img/prev.gif'/></a>"; 

    if ($page<$maxPage) { 
    $nextPage=($page+1)>$maxPage?$maxPage:$page+1;
    echo "<a href='?ziduan={$ziduan}&txt_sousuocur=".rawurlencode($txt_sousuo)."&curPage=$nextPage'><img src='img/next.gif'/></a>"; 

    echo "</table>"; 
    echo "<div align='left' class='shu'>共有".$maxPage."页&nbsp;当前为第".$page."页";
    ?>