简体   繁体   中英

Filter SQL results by date using PHP

I am getting data from an SQL table via PHP to populate an HTML table. This is how I do it:

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

Now what I want to do is, to set a filter to this. I want to add two date selectors and when the user clicks the Filter button, the table should be populated only with posts posted within the two given dates.

I thought of using a form for the date selector:

<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 to handle that data:

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

I was wondering if there is a way to include this $filter in the $respost mysql query like this:

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

Also note that the $uid is taken from a previous page using the $_GET[] method. So the this page's link looks like www.mysite.com/page?uid=66 . So wondering if using POST and form to filter will work?

In general if you do POST, its better to add uid as hidden field in youf second form in order to keep it. Also use BETWEEN in your filter, so the code should look like this:

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

Update: By the way, there is a small form 'hack' which allows you to use both GET and POST vars (insead of using hidden field), but is not a good practice at all, so I not recommend it:

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

when you submit this form, you will have both GET and POST arrays assigned, ie you will have uid as GET var, all others will be into POST array.

You must add your url value in hidden field like this

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>

You must add a variable for to output query

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

I think it solves your problem

Complete Code With Date Filter

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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