PHP:带有提交按钮的分页 [英] PHP: Pagination with submit button

查看:41
本文介绍了PHP:带有提交按钮的分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用分页显示数据库记录,因此我遵循了 php paging教程及其工作正常.现在我想显示两个日期之间差异的记录.我的 html 就像

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

当我尝试上面的代码时,第一次显示 10 条记录,接下来显示空屏幕.我在网上搜索,但无法找到这个问题的原因.请帮助我实现这一目标.

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.

推荐答案

使用 $_REQUEST 而不是 $_POST 因为您对下一个和最后一个链接使用标签.您需要在标签链接中传递 from 和 to 日期.

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.

顶部 if 条件:

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

这篇关于PHP:带有提交按钮的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆