搜索结果分页不起作用

时间:2014-09-20 05:00:06

标签: php search pagination

我正在尝试搜索数据后进行分页。在第一页数据显示,但如果点击下一页或页码,则不显示任何内容。如果回到第一页,那么数据也会消失。请帮我解决这个问题。

由于

<FORM NAME="form1" METHOD="post" ACTION="">
    <input type="text" name="university" value=""  class="bgnone" >    
    <input name="search" value="search" class="bgnone" type="submit" ></SPAN>        
</form>

<?php
    include('connect.php'); 
    $number=0;
    if($_POST['search']){           
        $uni=$_POST['university'];

        $per_page =10;
        $page_num = 1;

        if(isset($_GET['page'])){
            if(is_numeric($_GET['page']))
                $page_num = $_GET['page'];
        }
        $start = ($page_num-1)*$per_page;

        $id=$_GET['id'];        
        $result = mysql_query("SELECT * FROM person where university = '$uni'");        
        /* $row_num = mysql_num_rows($result1);*/

        $result_search= mysql_query(
            "SELECT * FROM person  where university = '$uni' order by id desc limit $start, $per_page");
        $row_num = mysql_num_rows($result);             
        $max_pages = ceil($row_num / $per_page);

        if(!$start){
            $start = 0;
        }                       
?>

<table>
    <tr>            
        <td>Name </td>
        <td>Email</td>
        <td>Address</td>
        <td>Phone</td>            
    </tr>

    <?php       
        while($row_prev= mysql_fetch_array($result_search)){
    ?>      
    <TR>         
        <TD BGCOLOR="#FFFFCC" CLASS="n12" WIDTH=130><?php echo $row_prev['name'] ;?></TD>    
        <TD BGCOLOR="#99FF66" ROWSPAN="2" NOWRAP CLASS="n12"> <?php echo $row_prev['email'] ;?></TD>    
        <TD BGCOLOR="#99FF66" NOWRAP CLASS="n12"><?php echo $row_prev['address'] ;?></TD>    
        <TD BGCOLOR="#FFFFCC" CLASS="n12" WIDTH=140><?php echo  $row_prev['phone'] ;?></TD>    
    </TR>    
    <?php   }   ?>
</table>

<?php
    $previous = $page_num - 1;
    $next = $page_num + 1;                
?>

<div id="pagination">    
    <div id="firstpage"> 
    <?php if($previous <= 0) { echo "<strong>First</strong>";} 
          else {echo "<a href='search.php?page=1'>First</a>";}
    ?>
    </div>

    <div id="previous">
    <?php if($previous <= 0) { echo "<strong>Previous</strong>";} 
          else { echo "<a href='search.php?page=$previous'>Previous</a>"; }
    ?></div>

    <div id="pagenumber" >
    <?php 
        for($i=1; $i<=$max_pages; $i++)
        {
           echo "<a href='search.php?page=$i'>$i |&nbsp;</a>";
        }
    ?>
    </div>

    <div id="next"> 
    <?php 
        if($next > $max_pages) { echo "<strong>Next</strong>";} 
        else {echo "<a href='search.php?page=$next'>Next</a>";}
    ?></div>

    <div id="last">
    <?php 
        if($next > $max_pages){ echo "<strong>Last</strong>";} 
        else {echo "<a href='search.php?page=$max_pages'>Last</a>";}
    ?></div>            
    </div>  
    <?php } ?>

3 个答案:

答案 0 :(得分:1)

请仔细检查完整的代码:

<FORM NAME="form1" METHOD="get" ACTION="">
  <input type="text" name="university" value=""  class="bgnone" >
  <input name="search" value="search" class="bgnone" type="submit" >
  </SPAN>
</form>

<table height="200px" style="border:3px black solid;border-radius:5px" width="550px">
<tr>
<th colspan="2" height="40px" style="border-bottom:3px black solid">User Name</th>
</tr>
<?php
    if(isset($_GET['university'])){
        $uni=$_GET['university'];
    $link=mysql_connect("localhost","root","");
    mysql_select_db("dbname",$link);
    $q="select count(*) \"total\"  from tablename where firstname='".$uni."'";
    $ros=mysql_query($q,$link) or die(mysql_error());
    $row=mysql_fetch_array($ros);
    $total=$row['total'];
    $dis=4;
    $total_page=ceil($total/$dis);
    $page_cur=(isset($_GET['page']))?$_GET['page']:1;
    $k=($page_cur-1)*$dis;

    $q="select * from tablename where firstname='".$uni."' limit $k,$dis";
    //echo "select * from tablename where firstname='".$uni."' limit $k,$dis";die;
    $ros=mysql_query($q,$link);
    while($row=mysql_fetch_array($ros))
    {
        echo '<tr>';
        echo '<td width="10px" style="border-bottom:1px #a1a1a1 solid">'.$row['id'].'.';
        echo '<td style="border-bottom:1px #a1a1a1 solid">'.$row['firstname'];
        echo '</tr>';
    }
    echo '</table>';
    echo '<br/>';
    if($page_cur>1)
    {
        echo '<a href="paging-ex.php?page='.($page_cur-1).'&university='.$uni.'" style="cursor:pointer;color:green;" ><input style="cursor:pointer;background-color:green;border:1px black solid;border-radius:5px;width:120px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button" value=" Previous "></a>';
    }
    else
    {
      echo '<input style="background-color:green;border:1px black solid;border-radius:5px;width:120px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value=" Previous ">';
    }
    for($i=1;$i<$total_page;$i++)
    {
        if($page_cur==$i)
        {
            echo ' <input style="background-color:green;border:2px black solid;border-radius:5px;width:30px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value="'.$i.'"> ';
        }
        else
        {
        echo '<a href="paging-ex.php?page='.$i.'&university='.$uni.'"> <input style="cursor:pointer;background-color:green;border:1px black solid;border-radius:5px;width:30px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button" value="'.$i.'"> </a>';
        }
    }
    if($page_cur<$total_page)
    {
        echo '<a href="paging-ex.php?page='.($page_cur+1).'&university='.$uni.'"><input style="cursor:pointer;background-color:green;border:1px black solid;border-radius:5px;width:90px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button" value=" Next "></a>';
    }
    else
    {
     echo '<input style="background-color:green;border:1px black solid;border-radius:5px;width:90px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value="   Next ">';
    }
    }
?>
</table>

答案 1 :(得分:0)

最好更换以下内容

if(is_numeric($_GET['page']))
                $page_num = $_GET['page'];
            }

通过

$page_num = int($_GET['page']);

答案 2 :(得分:0)

那么,为了让您的SQL查询正常运行,您需要$_POST['search']true$uni = $_POST['university']为非空,对吧?目前,没有任何信息通过点击下一页或页码传递给您的search.php

因此,您可能希望将分页<a>标记更改为以下内容:

<a href='search.php?page=$next&university=$uni&search=true'>Next</a>

并替换代码的顶部以使用$_REQUEST

if($_REQUEST['search']){           
    $uni=$_REQUEST['university'];
    ....

希望有所帮助。