Tanziah  Khanam
Tanziah Khanam

Reputation: 37

Search result pagination does not work

I am trying to make pagination after searching data. In first page data are showing, but if click on next page or page number nothing is showing. If back to first page then also data disappear.please help me in this issue.

Thanks

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

Upvotes: 0

Views: 143

Answers (3)

truesource
truesource

Reputation: 397

Well check the complete code :

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

Upvotes: 2

ivan.sim
ivan.sim

Reputation: 9268

Well, for your SQL queries to run correctly, you will need $_POST['search'] to be true and $uni = $_POST['university'] to be non-empty, right? At the moment, none of this information are being passed to your search.php by either clicking on next page or a page number.

So you might want to change your pagination <a> tags to something that looks like:

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

And replace the top part of your codes to use $_REQUEST:

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

Hope that helps.

Upvotes: 0

user1914292
user1914292

Reputation: 1556

You'd be better off replacing the following

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

by

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

Upvotes: 0

Related Questions