PHP: Pagination with submit button

2019-09-07 11:59发布

问题:

I am trying to display the db records using pagination and hence i followed php paging tutorial and its working fine. Now i would like to display records from the difference between 2 dates. my html is like

<html>
<body>
    <form  method="post" name="Form1" >
        Startdate : <input name="startdate" type="text"  id="datepicker"  />
        Enddate : <input name="enddate" type="text"  id="datepicker1"  />
        <input type="submit" name="submit"  value="submit"  />
    </form>
</body>

I tried the above code inside my submit button as

<?php
if (isset($_POST['submit'])) 
{
    include 'connection.php'; 

    $startdate=$_POST['startdate']; 
    $enddate=$_POST['enddate'];     


    $rec_limit = 10;
    /* Get total number of records */
    $sql   = "SELECT COUNT(*) as num FROM tblname";
    $retval= mysql_query( $sql, $conn );
    if(! $retval )
    {
      die('Could not get data: ' . mysql_error());
    }

    $row      = mysql_fetch_array($retval, MYSQL_NUM );
    $rec_count= $row[0];

    if( isset($_GET{'page'}) )
    {
        $page  = $_GET{'page'} + 1;
        $offset= $rec_limit * $page ;
    }
    else
    {
        $page  = 0;
        $offset= 0;
    }

    $left_rec= $rec_count - ($page * $rec_limit);
    $sql     = "SELECT * FROM tblname Where start = '$startdate' AND end <= 'enddate' LIMIT $offset, $rec_limit ";  
    $retval  = mysql_query( $sql, $conn );

    if(! $retval )
    {
        die('myquery table failed: ' . mysql_error());
    }       

    echo "<table  border=1 >"; // start a table tag in the HTML
    echo "<tr>";
    echo "<td> Start </td>";
    echo "<td>End</td>";

    echo "</tr>";

    while($row = mysql_fetch_assoc($retval))
    {
        echo "<tr><td>{$row['start']}  <br> </td>".
             "<td>{$row['end']} <br> </td>";
    } 

    if( $page > 0 )
    {
        $last = $page - 2;
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$last\">Last</a> |";
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$page\">Next</a>";    

        echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}?page=$page\">";
        echo '<input type="submit" value="Next" class="button">';
        echo '</form>';
    }
    else if( $page == 0 )
    {
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$page\">Next</a>";
    }
    else if( $left_rec < $rec_limit )
    {
        $last = $page - 2;
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$last\">Last</a>";
    }

    mysql_close($conn);
}
?>

When I try the above code, first time 10 records are displaying and next it is showing empty screen. I am searching in net but cannot able to find the reason for this problem . Kindly help me in achieving this.

回答1:

Use $_REQUEST instead of $_POST because you use a tag for next and last links. You need to pass from and to dates in your a tag links.

Top if condition :

if (isset($_REQUEST['startdate']) && isset($_REQUEST['enddate'])){
 include 'connection.php'; 

 $startdate=$_REQUEST['startdate']; 
 $enddate=$_REQUEST['enddate'];

 //your codes
}

add dates in Next and Prev links like below

echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$last&startdate=$startdate&enddate=$enddate\">Last</a> |";