【问题标题】:Filter SQL results by date using PHP使用 PHP 按日期过滤 SQL 结果
【发布时间】:2016-07-27 20:25:52
【问题描述】:

我通过 PHP 从 SQL 表中获取数据以填充 HTML 表。我就是这样做的:

<table class="posts">
        <?php 
        $respost = mysqli_query($link,"SELECT * FROM table WHERE post_author=$uid LIMIT 16 ");
        $row_count=0;
        $col_count=0;
        while($rowpost = mysqli_fetch_array($respost)) {
            if($row_count%4==0){
                echo "<tr>";
                $col_count=1;
            }?>
            <td>
            <?php
            $imageid = $rowpost['thumbnail_link'];
            <img src="<?php echo $imageid; ?>" alt="" class="img-responsive" height="220px" height="220px">
            <h3><?php echo $rowpost['post_title']; ?></h3>
            <h4><?php echo (substr($rowpost['post_excerpt'],0,30)); ?></h4>
            <h5><?php echo $rowpost['post_date']; ?></a></h5>
            </td>
            <?php 
            if($col_count%4==0){
                echo "</tr>";
            }
            $row_count++; 
            $col_count++; 
        }
        ?>
</table>

现在我想做的是,为此设置一个过滤器。我想添加两个日期选择器,当用户单击过滤器按钮时,表格应该只填充两个给定日期内发布的帖子。

我想为日期选择器使用表单:

<form name="filter" method="POST" action="team_as.php">
    SHOW POSTS FROM:
        <input type="date" name="sdate">
        <input type="date" name="edate">
        <input type="submit" name="submit" value="Filter">
</form>

PHP 处理该数据:

$edate = $_POST['edate'];
$sdate = $_POST['sdate'];
$filter = "AND $date < post_date > $edate";

我想知道是否有办法将这个$filter 包含在$respost mysql 查询中,如下所示:

$respost = mysqli_query($link,"SELECT * FROM table WHERE post_author=$uid $filter LIMIT 16 ");

另请注意,$uid 是使用 $_GET[] 方法从上一页获取的。所以这个页面的链接看起来像www.mysite.com/page?uid=66。那么想知道使用 POST 和表单进行过滤是否可行?

【问题讨论】:

    标签: php html mysql datetime


    【解决方案1】:

    一般来说,如果你做 POST,最好在你的第二个表单中添加 uid 作为隐藏字段以保留它。在您的过滤器中也使用 BETWEEN,因此代码应如下所示:

        <form name="filter" method="POST" action="team_as.php">
            <input type="hidden" name="uid" value="<?php echo $uid; ?>">
            SHOW POSTS FROM:
            <input type="date" name="sdate">
            <input type="date" name="edate">
            <input type="submit" name="submit" value="Filter">
        </form>
        <?php
        if(isset($_POST['submit'])){
            $uid = $_POST['uid'];
            $edate = $_POST['edate'];
            $sdate = $_POST['sdate'];
            $filter = "AND DATE(post_date) BETWEEN '$sdate' AND '$edate'";
            $query = "SELECT * FROM table WHERE post_author=$uid $filter LIMIT 16 ";
            $respost = mysqli_query($link, $query);
        }else{
            $query = "SELECT * FROM table WHERE post_author=$uid LIMIT 16 ";
            $respost = mysqli_query($link, $query);            
        }
        ?>
    

    更新:顺便说一句,有一个小表单“hack”允许您同时使用 GET 和 POST 变量(而不是使用隐藏字段),但这根本不是一个好习惯,所以我不推荐它:

    <form name="filter" method="POST" action="team_as.php?uid=<?php echo $uid; ?>">
    

    当您提交此表单时,您将同时分配 GET 和 POST 数组,即您将有 uid 作为 GET var,所有其他人都将进入 POST 数组。

    【讨论】:

    • 我添加了这个并提交,页面刷新链接末尾没有?uid=66,仍然显示与以前相同的结果。没有过滤。 uid 很重要,因为页面应该只显示该用户完成的帖子。另外仅供参考,post_date 采用以下格式:2014-03-11 12:25:41
    • 是的,因为您正在执行 POST。但是uid 变量将在那里,因此您可以像往常一样使用它。检查这一点的最简单方法是执行echo $respost; 以检查整个查询的外观。关于日期格式,我在$filter 部分更新了我的代码。
    • ">
    • @YohanBlake,我已经将有关如何使用 GET 和 POST 的小更新放在一起,但同样,这不是一个好习惯。
    • @Chay22,你的评论与这个话题有什么关系?
    【解决方案2】:

    您必须像这样在隐藏字段中添加您的 url 值

    if(isset($_GET['uid']))
    {
      $uid=mysqli_real_escape_string($_GET['uid']);
    }  
    <form name="filter" method="POST" action="team_as.php">
    SHOW POSTS FROM:
        <input type="date" name="sdate">
        <input type="date" name="edate">
        <input type="hidden" name="uid" value="<?php echo $uid; ?>">
        <input type="submit" name="submit" value="Filter">
    </form>
    

    您必须为输出查询添加一个变量

     if(isset($_POST['submit']))
     {
      $uid=mysqli_real_escape_string($_POST['uid']);
      $edate = mysqli_real_escape_string($_POST['edate']);
      $sdate = mysqli_real_escape_string($_POST['sdate']);
     }
     $sql="SELECT * FROM table WHERE post_author='$uid'";
     if(isset($_POST['submit']))
      {
       $sql.= " AND $date < post_date > $edate";
      }  
     $respost = mysqli_query($link,$sql);
    

    我认为它解决了你的问题

    【讨论】:

      【解决方案3】:

      带有日期过滤器的完整代码

      <div class="table-responsive m-t-10">
                                          <form class="form-group" method='post' style="margin-bottom: -5px;">
                                Start Date <input class="form-group" type='date' class='dateFilter' name='dateFrom' value='<?php if(isset($_POST['dateFrom'])) echo $_POST['dateFrom']; ?>'>
      
                                End Date <input class="form-group" type='date' class='dateFilter' name='dateTo' value='<?php if(isset($_POST['dateTo'])) echo $_POST['dateTo']; ?>'>
      
                                <input type='submit' name='but_search' value='Search'>
                              </form> 
                                             <table id="myTable"  class="table table-bordered table-striped dataTable no-footer" role="grid">
                                                  <!--<table class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">-->
                                                  <thead>
                                                      <tr>
                                                        <th>Date</th>
                                                        <th>Customer Name</th>
                                                        <th>Company</th>
                                                        <th>Userid</th>
                                                        <th>Address</th>
                                                        <th>Area</th>
                                                        <th>Contact</th>
                                                        <th style="display:none">Location</th>
                                                        <th style="display:none">Product Id</th>
                                                        <th>Software Id</th>
                                                        <th>Product Key</th>
                                                        <th>Validity</th>
                                                        <th>Unpaid Amount</th>
                                                        <th>Action</th>
                                                        <th>Enab/Disa</th>
                                                        <th >Delete</th>
                                                      </tr>
                                                  </thead>
                                                  <tfoot style="display:none">
                                                      <tr>
                                                          <th>Date</th>
                                                          <th>Name</th>
                                                          <th>Userid</th>
                                                          <th>Address</th>
                                                          <th>Area</th>
                                                          <th>Contact</th>
                                                          <th>Software</th>
                                                          <th>Location</th>
                                                          <th>Product Id</th>
                                                          <th>Validity</th>
                                                          <th>Unpaid Amount</th>
                                                          <th>Action</th>
                                                          <th>Enab/Disa</th>
                                                          <th>Delete</th>
                                                      </tr>
                                                  </tfoot>
                                                  <tbody>
                                                  <?php
                                                $ses = $_SESSION["userid"];
                                                $result = mysqli_query($con, "select id from cmp_user where userid='$ses'");
                                                while ($res = mysqli_fetch_array($result)) {
                                                    $uid =$res['id'];
                                                }
                                                ?>
      
                                                          <?php
                                                           $dateFrom = date('Y-m-d', strtotime($_POST['dateFrom']));
                                                           $dateTo = date('Y-m-d', strtotime($_POST['dateTo']));
                                                          $result= mysqli_query($con, "select * from cmp_customer");
                                                          while ($res = mysqli_fetch_array($result)) {
                                                              if (date('Y-m-d', strtotime($res['date']))>$dateFrom && date('Y-m-d', strtotime($res['date'])) < $dateTo) {
                                                                  ?>
                                                              echo "<tr>"; ?>
                                                          <td><?php echo $res["date"]; ?></td>
                                                          <td><?php echo $res["c_name"]; ?></td>
                                                          <td><?php echo $res["company"]; ?></td>
                                                          <td><?php echo $res["userid"]; ?></td>
                                                          <td><?php echo substr($res["address"], 0, 15); ?></td>
                                                          <td><?php echo substr($res["zipcode"], 0, 15); ?></td>
                                                          <td><?php echo $res["contact"]; ?></td>
                                                          <td style="display:none"><?php echo $res["location"]; ?></td>
                                                          <td style="display:none"><?php echo substr($res["productid"], 0, 15); ?></td>
                                                          <td><?php echo substr($res["softwareid"], 0, 15); ?></td>
                                                          <td><?php echo substr($res["productkey"], 0, 15); ?></td>
                                                          <td><?php echo $res["Validity"]; ?></td>
                                                          <td><?php echo $res["unpaidamt"]; ?></td>
                                                          <td><A class='btn btn-outline-success btn-sm' href="update_customer.php?id=<?php echo $res['id']; ?>">Edit</A></td>
                                                          <?php if ($res["status"] == 1) {
                                                                      ?>
                                                               <td> <a class='btn btn-success btn-sm' onclick="return confirm('Are you sure want Disable this Customer ?')" href="customerstatusdisable.php?userid=<?php echo $res['userid']; ?>">Enable
                                                              </a></td>
                                                              <?php
                                                                  } else { ?>
                                                           <td><a class='btn btn-danger btn-sm' onclick="return confirm('Are you sure want Enable this Customer ?')" href="customerstatus.php?userid=<?php echo $res['userid']; ?>">Disable
                                                              </a></td>
      
                                                               <?php } ?>
                                                          <td><a class='btn btn-outline-danger btn-sm' onclick="return confirm('Are you sure want delete this Customer ?')" href="delete_customer.php?id=<?php echo $res['id']; ?>">Delete
                                                              <i class="fa fa-trash" title="Delete" ></i></a></td>
                                                        <?php echo "</tr>";
                                                              }
                                                          } ?>
      
      
                                                  </tbody>
                                              </table>
                                          </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多