我有一个使用PHP的工作日期过滤器。问题是如果我不过滤我的记录,那么就不会显示任何记录,而且我也会遇到这种类型的错误:
注意:未定义的索引:第37行的...中的dateFrom
如何使用PHP创建日期过滤器,这样如果我没有向我的过滤器添加任何日期,它会显示所有记录,如果我添加日期来过滤它只显示过滤后的记录?
这是我的代码:
<?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>
答案 0 :(得分:1)
您需要更改date('Y-m-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();
检查变量,你的代码应该是这样的
<?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 }
}
}
?>
答案 1 :(得分:0)
带有日期过滤器的完整代码
<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>