簡體   English   中英

如何使用PHP按日期過濾記錄

[英]How to filter records by date with PHP

我有一個使用PHP的工作日期過濾器。 問題是,如果我不過濾我的記錄,則不會顯示任何記錄,並且還會出現這種類型的錯誤:

注意:未定義的索引:dateFrom in ...在第37行

如何使用PHP創建日期過濾器,以便在不向過濾器添加任何日期的情況下顯示所有記錄,而在添加過濾器的日期僅顯示過濾的記錄呢?

這是我的代碼:

HTML表格 在此處輸入圖片說明

<?php
    include('config/connect.php');
    $query=mysqli_query($connect,"SELECT * FROM tasks");
?>

<table>
    <tr>
        <th>Title</th>
        <th>Due Date</th>
        <th>Edit | View</th>
    </tr>

    <?php

    //GETTING VALUE FROM FILTER
    $new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
    //echo $new_date;

    $new_date2 = date('Y-m-d', strtotime($_POST['dateTo']));
    //echo $new_date2;

    $dateFrom = date('Y-m-d', strtotime($_POST['dateFrom']));
    $dateTo = date('Y-m-d', strtotime($_POST['dateTo']));

    while($row=mysqli_fetch_array($query)){

    //FILTERING USING DATES
    if (($row['due_date'] > $dateFrom) && ($row['due_date'] < $dateTo)){
    ?>

    <tr>
        <td><?php echo  $row['title'] ?></td>
        <td><?php echo  $row['due_date'] ?></td>
        <td><a href="edit_task.php?id=<?php echo $row['id'];?>"> <button type="button">Edit</button> </a> | <a href="view_task.php?id=<?php echo $row['id'];?>"> <button type="button">View</button> </a></td>
    </tr>

    <?php }} ?>

</table>

您需要更改date('Ym-d', strtotime($row['due_date'])); 而是像這樣的$row['due_date']

if (date('Y-m-d', strtotime($row['due_date']))>$dateFrom && date('Y-m-d', strtotime($row['due_date'])) < $dateTo){

並且您需要首先通過isset();檢查變量isset(); 您的代碼應該像這樣

<?php
if (isset($_POST['dateFrom'])) {


    //GETTING VALUE FROM FILTER
    $new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
    //echo $new_date;

    $new_date2 = date('Y-m-d', strtotime($_POST['dateTo']));
    //echo $new_date2;

    $dateFrom = date('Y-m-d', strtotime($_POST['dateFrom']));
    $dateTo = date('Y-m-d', strtotime($_POST['dateTo']));

    while($row=mysqli_fetch_array($query)){

    //FILTERING USING DATES
    if (date('Y-m-d', strtotime($row['due_date']))>$dateFrom && date('Y-m-d', strtotime($row['due_date'])) < $dateTo){
    ?>

    <tr>
        <td><?php echo  $row['title'] ?></td>
        <td><?php echo  $row['due_date'] ?></td>
        <td><a href="edit_task.php?id=<?php echo $row['id'];?>"> <button type="button">Edit</button> </a> | <a href="view_task.php?id=<?php echo $row['id'];?>"> <button type="button">View</button> </a></td>
    </tr>

    <?php }
} 

}
?>

帶有日期過濾器的完整代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM