2014-09-20 180 views
0

我試圖在搜索數據後進行分頁。在第一頁數據顯示,但如果點擊下一頁或頁碼沒有顯示。如果回到第一頁,那麼數據也會消失。請在這個問題上幫助我。搜索結果分頁不起作用

感謝

<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 } ?> 

回答

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> 
+0

感謝您help.It工作。 – 2014-09-20 09:00:29

0

你會更好更換以下

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

通過

$page_num = int($_GET['page']); 
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']; 
    .... 

希望有所幫助。