繁体   English   中英

PHP MySQL表的日期范围过滤器

[英]Date Range Filter for PHP MySQL Table

我是PHP的新手,我只是想问。 如何设置日期范围过滤器? 我已经尝试了教程 - 但它们似乎并不适合我。

我有这个,但它没有功能。 我正在尝试在CRUD表中实现它。

<form role="form" action="index.php" method="get">
    <div class="form-group">
    <input placeholder="Filter From" class="col-sm-2"  type="text" id="from" name="from">
    <input placeholder="To" class="col-sm-2"  type="text" id="to" name="to">
    <input style="padding-bottom:0px" class="btn btn-default" type="submit" name="go" value="filter range">
    </div>
</form>

对于日期选择器,我使用它。

<script>
$(function(){

    $("#from").datepicker({
        defaultDate:"+lw",
        changeMonth: true,
        numberOfMonths:3,
        onClose:function(selectedDate){
            $("#to").datepicker("option","minDate",selectDate);
        }
    });

    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth:true,
        numberOfMonths: 3,
        onClose: function(selectedDate){
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});
</script>

PHP表,我正在尝试实现这个..

<?php  
 $connect = mysqli_connect("localhost", "root", "root", "appointments");  
 $output = '';  
 $sql = "SELECT * FROM appointments ORDER BY id DESC";  
 $result = mysqli_query($connect, $sql);  
 $output .= '

      <div class="table-responsive">

           <table class="table table-bordered">  
                <tr>  
                     <th>Select</th>  
                     <th width="10%">Id</th>  
                     <th width="40%">Name</th>  
                     <th width="40%">Email</th>  
                     <th width="40%">Address</th>  
                     <th width="10%">phoneNumber</th>  
                     <th width="10%">appointmentTime</th> 
                     <th width="10%">appointmentDate</th> 
                     <th width="50%">message</th> 
                     <th width="10%">delete</th>

                </tr>';  
                             if(mysqli_num_rows($result) > 0)  
                             {  
                                  while($row = mysqli_fetch_assoc($result))  
                                  {  
                                       $output .= '               

                    <tr id="'.$row["id"].'"> 

                    <td><input type="checkbox" name="id" value="'.$row["id"].'"></td>
                     <td>'.$row["id"].'</td>
                     <td class="name" data-id1="'.$row["id"].'" contenteditable>'.$row["name"].'</td>  
                     <td class="email" data-id2="'.$row["id"].'" contenteditable>'.$row["email"].'</td>
                     <td class="address" data-id2="'.$row["id"].'" contenteditable>'.$row["address"].'</td>  
                     <td class="phoneNumber" data-id2="'.$row["id"].'" contenteditable>'.$row["phoneNumber"].'</td>  
                     <td class="appointmentTime" data-id2="'.$row["id"].'" contenteditable>'.$row["appointmentTime"].'</td>  
                     <td class="appointmentDate" data-id2="'.$row["id"].'" contenteditable>'.$row["appointmentDate"].'</td>
                     <td class="message" data-id2="'.$row["id"].'" contenteditable>'.$row["message"].'</td>
                     <td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger                                               btn_delete">Delete This Row</button></td>
                </tr>  
              ';  
      }                           

 }  
 else  
 {  

      $output .= '<tr><td colspan="10">Data not Found</td></tr>';  
 }  


 $output .= '</table>
<div align="center"> 
<button type="button" name="delete" id="delete">Delete</button>
                </div>  
      </div>   ';  
 echo $output;

 ?>  

您应该获得日期范围的值,以便根据输入的日期范围确定预约日期的范围。

试试这个吧!

PHP:

<?php

//get date range value
if (isset($_GET['go'])) {

   $date_from   = $_GET['from'];
   $date_to     = $_GET['to'];
}

SQL:

$sql = "SELECT * 
            FROM appointments 
            WHERE appointmentDate 
            BETWEEN  '$date_from' AND '$date_to'
            ORDER BY id 
            DESC"; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM