abcd abcd - 10 days ago 4
HTML Question

Filter results based on From & To Date

I am trying below code to filter values based on From & to Date , but when i click on search button , its just refreshing page and displaying same page.
But not displaying filtered results....

enter image description here

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

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


column name of date :
created_at
, table name -
order_details
. i am completely new to php , please help me.

Edit

when i tried
var_dump($post_at); var_dump($post_at_to_date);
, it gave result :
string(0) "" string(0) ""


Edit 2 i am trying below code, now var_dump is displaying results, but the values are not filtering based on selected From & To Date

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

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


script

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


result :

Array
(
[post_at] =>
[post_at_to_date] => 2016-11-17
[search] => search
)


24result found

Notice: Undefined index: id in
echo "id: " . $row["id"]. " - date: " . $row["post_at"];
& displaying same page.

id: - date: 0000-00-00 00:00:00

Answer

Providing that your date fields in your database are date and not datetime then this should work, you are currently using invalid datetimes.

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

Some date formatting errors, have a look at the var_dump( $sqlquery ); and make sure that the formatting is correct. Then make sure that the result is coming through.

Comments