[英]Filter results based on From & To Date
我正在尝试下面的代码来基于“从&到日期”过滤值,但是当我单击“搜索”按钮时,它只是刷新页面并显示同一页面。 但不显示过滤结果。
的PHP
$reg_user='';
$post_at = "";
$post_at_to_date = "";
$queryCondition = "";
if(!empty($_POST["search"]["post_at"]))
{
$post_at = date('Y-m-d 0:0:0',strtotime($_POST["search"]["post_at"]));
$post_at_todate = date('Y-m-d:59:59:59');
if(!empty($_POST["search"]["post_at_to_date"])) {
$post_at_to_date = date('Y-m-d 59:59:59',strtotime($_POST["search"]["post_at_to_date"]));
$post_at_todate = $post_at_to_date;
}
$queryCondition .= "WHERE post_at >= '" . $post_at . "' AND post_at <= '" . $post_at_todate . "'";
$reg_user = new USER();
$stmt = $reg_user->runQuery("SELECT * FROM order_details ".$queryCondition);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->execute();
html
<form name="frmSearch" method="post" action="">
<input type="text" placeholder="From Date" id="post_at"
value="<?php echo $post_at; ?>" name="search[post_at]" />
<input type="text" placeholder="To Date" id="post_at_to_date" value="<?php echo $post_at_to_date; ?>"
name="search[post_at_to_date]" onkeyup="doFilter()" />
<input type="submit" name="search" value="search" >
脚本
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src= "http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js"></script>
<script>
$.datepicker.setDefaults({
showOn: "button",
buttonImage: "datepicker.png",
buttonText: "Date Picker",
buttonImageOnly: true,
dateFormat: 'yy-mm-dd'
});
$(function() {
$("#post_at").datepicker();
$("#post_at_to_date").datepicker();
});
</script>
日期的列名: created_at
,表名order_details
。 我对php完全陌生,请帮助我。
编辑
当我尝试var_dump($post_at); var_dump($post_at_to_date);
var_dump($post_at); var_dump($post_at_to_date);
,结果为: string(0) "" string(0) ""
编辑2我正在尝试下面的代码,现在var_dump正在显示结果,但是值未基于选定的“从&到日期”进行过滤
echo "<pre>";print_r($_POST);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$reg_user='';
$post_at = "";
$post_at_to_date = "";
$queryCondition = "";
if(!empty($_POST["post_at"]))
{
$post_at = date('Y-m-d 0:0:0',strtotime($_POST["post_at"]));
$post_at_todate = date('Y-m-d:59:59:59');
if(!empty($_POST["post_at_to_date"])) {
$post_at_to_date = date('Y-m-d 59:59:59',strtotime($_POST["post_at_to_date"]));
$post_at_todate = $post_at_to_date;
}
$queryCondition .= "WHERE post_at >= '" . $post_at . "' AND post_at <= '" . $post_at_todate . "'";
}
$sqlquery = "SELECT * FROM order_details $queryCondition";
$result = $conn->query($sqlquery);
if ($result->num_rows > 0) {
echo $result->num_rows.'result found'."<br>";
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - date: " . $row["post_at"];
echo "<br>";
}
} else {
echo "0 results found <br>";
}
$conn->close();
形成
<form name="frmSearch" method="post" action="">
<input type="text" placeholder="From Date" id="post_at" value="" name="post_at" />
<input type="text" placeholder="To Date" id="post_at_to_date" value="" name="post_at_to_date" />
<input type="submit" name="search" value="search" />
</form>
脚本
jQuery.datepicker.setDefaults({
showOn: "button",
buttonImage: "datepicker.png",
buttonText: "Date Picker",
buttonImageOnly: true,
dateFormat: 'yy-mm-dd'
});
$(function() {
$("#post_at").datepicker();
$("#post_at_to_date").datepicker();
});
结果:
Array
(
[post_at] =>
[post_at_to_date] => 2016-11-17
[search] => search
)
找到24个结果
注意:未定义的索引:id in echo "id: " . $row["id"]. " - date: " . $row["post_at"];
echo "id: " . $row["id"]. " - date: " . $row["post_at"];
并显示同一页面。
id:-日期:0000-00-00 00:00:00
试试CAST:
$ queryCondition。= “WHERE CAST( post_at
如DATE)> = CAST(”。$ post_at。 “作为日期)和CAST( post_at
如DATE)<= CAST(”。$ post_at_todate。 “为DATE)”
假设您数据库中的日期字段是date
而不是datetime
date
,那么这应该起作用,您当前正在使用无效的日期时间。
if( !empty( $_POST[ 'post_at' ] ) )
{
$post_at = date( 'Y-m-d', strtotime( $_POST[ 'post_at' ] ) );
$post_at_todate = date( 'Y-m-d' );
if( !empty( $_POST[ 'post_at_to_date' ] ) )
{
$post_at_to_date = date( 'Y-m-d', strtotime( $_POST[ 'post_at_to_date' ] ) );
$post_at_todate = $post_at_to_date;
}
$queryCondition .= "WHERE DATE( created_at ) >= '" . $post_at . "' AND DATE( created_at ) <= '" . $post_at_todate . "'";
}
$sqlquery = "SELECT * FROM order_details $queryCondition";
一些日期格式错误,请查看var_dump( $sqlquery );
并确保格式正确。 然后确保结果通过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.