当日期范围输入为空时显示所有数据
Display all data when Date Range inputs are null
我有一个带有日期范围过滤器的数据网格,我有两个日期输入,一个用于 'fromDate' 参数,另一个用于 'toDate' 参数,这允许我显示数据在两个日期之间。
我想在应用任何过滤器之前显示我的所有数据。 datagrid 只允许我在应用过滤器或插入 'toDate' 参数时显示数据,否则,当两个参数均为 null 时,datagrid 将不会显示任何数据。
我读过另一个与此类似的问题,但它没有解决我的问题:
A single sql query which can handle both null or valued date range in sql server
此外,我还使用了其他类型的过滤器,即使使用空参数也能正常工作。
脚本:
<script type="text/javascript">
function doSearch(){
$('#tt').datagrid('load',{
fromDate: $('#fromDate').val(),
toDate: $('#toDate').val(),
});
}
</script>
HTML:
<table id="tt" class="easyui-datagrid" style="width:1000px;height:450px"
url="datagrid24_getdata.php" sortName="id" sortOrder="asc"
title="Search" iconCls="icon-search" toolbar="#tb"
rownumbers="true" pagination="true">
<thead>
<tr>
<th field="id" width="50" sortable="true">ID</th>
<th field="userName" width="250" sortable="true">Name</th>
<th field="date" width="100" sortable="true">Date</th>
</tr>
</thead>
</table>
<div id="tb" style="padding:3px">
<span> Insert filter Parameters:</span>
<!-- DATE RANGE FILTER -->
<input id="fromDate" type="date" style="line-height:26px;border:1px solid #ccc">
<input id="toDate" type="date" style="line-height:26px;border:1px solid #ccc">
<a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">Search</a>
</div>
我的 PHP datagrid24_getdata.php :
<?php
include 'conectionbd.php';
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id';
$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
$offset = ($page-1)*$rows;
$result = array();
//HERE WE FETCH THE VALUES FROM THE FILTER INPUTS
$fromDate= isset($_POST['fromDate']) ? mysql_real_escape_string($_POST['fromDate']) : '';
$toDate= isset($_POST['toDate']) ? mysql_real_escape_string($_POST['toDate']) : '';
//THIS IS THE SQL QUERY I USE FOR THE DATE RANGE FILTER
$where = "date BETWEEN '$fromDate%' AND '$toDate%'";
$rs = mysql_query("select count(*) from table where " . $where);
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("select * from table where " . $where . " order by $sort $order limit $offset,$rows");
$items = array();
while($row = mysql_fetch_object($rs)){
array_push($items, $row);
}
$result["rows"] = $items;
echo json_encode($result);?>
非常感谢任何帮助!
您可以使用以下代码来处理日期条件:
$where = '';
if (!empty($fromDate) && !empty($toDate)) {
$where = "date BETWEEN '$fromDate%' AND '$toDate%'";
} else if(!empty($fromDate)) {
$where = "date >= '$fromDate%'";
} else if(!empty($toDate)) {
$where = "date <= '$toDate%'";
}
您可以这样做(我不太了解 PHP,因此可能需要更正检查日期值是否为空的语法):
//THIS IS THE SQL QUERY I USE FOR THE DATE RANGE FILTER
$where = '';
// check if both the dates are not null
if ($fromDate != null && $toDate != null) {
$where = "where date BETWEEN '$fromDate%' AND '$toDate%'";
} else if ($fromDate != null) {
$where = "where date >= '$fromDate%'";
} else if ($toDate != null) {
$where = "where date <= '$toDate%'";
}
$rs = mysql_query("select count(*) from table " . $where);
我有一个带有日期范围过滤器的数据网格,我有两个日期输入,一个用于 'fromDate' 参数,另一个用于 'toDate' 参数,这允许我显示数据在两个日期之间。
我想在应用任何过滤器之前显示我的所有数据。 datagrid 只允许我在应用过滤器或插入 'toDate' 参数时显示数据,否则,当两个参数均为 null 时,datagrid 将不会显示任何数据。
我读过另一个与此类似的问题,但它没有解决我的问题: A single sql query which can handle both null or valued date range in sql server
此外,我还使用了其他类型的过滤器,即使使用空参数也能正常工作。
脚本:
<script type="text/javascript">
function doSearch(){
$('#tt').datagrid('load',{
fromDate: $('#fromDate').val(),
toDate: $('#toDate').val(),
});
}
</script>
HTML:
<table id="tt" class="easyui-datagrid" style="width:1000px;height:450px"
url="datagrid24_getdata.php" sortName="id" sortOrder="asc"
title="Search" iconCls="icon-search" toolbar="#tb"
rownumbers="true" pagination="true">
<thead>
<tr>
<th field="id" width="50" sortable="true">ID</th>
<th field="userName" width="250" sortable="true">Name</th>
<th field="date" width="100" sortable="true">Date</th>
</tr>
</thead>
</table>
<div id="tb" style="padding:3px">
<span> Insert filter Parameters:</span>
<!-- DATE RANGE FILTER -->
<input id="fromDate" type="date" style="line-height:26px;border:1px solid #ccc">
<input id="toDate" type="date" style="line-height:26px;border:1px solid #ccc">
<a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">Search</a>
</div>
我的 PHP datagrid24_getdata.php :
<?php
include 'conectionbd.php';
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id';
$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
$offset = ($page-1)*$rows;
$result = array();
//HERE WE FETCH THE VALUES FROM THE FILTER INPUTS
$fromDate= isset($_POST['fromDate']) ? mysql_real_escape_string($_POST['fromDate']) : '';
$toDate= isset($_POST['toDate']) ? mysql_real_escape_string($_POST['toDate']) : '';
//THIS IS THE SQL QUERY I USE FOR THE DATE RANGE FILTER
$where = "date BETWEEN '$fromDate%' AND '$toDate%'";
$rs = mysql_query("select count(*) from table where " . $where);
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("select * from table where " . $where . " order by $sort $order limit $offset,$rows");
$items = array();
while($row = mysql_fetch_object($rs)){
array_push($items, $row);
}
$result["rows"] = $items;
echo json_encode($result);?>
非常感谢任何帮助!
您可以使用以下代码来处理日期条件:
$where = '';
if (!empty($fromDate) && !empty($toDate)) {
$where = "date BETWEEN '$fromDate%' AND '$toDate%'";
} else if(!empty($fromDate)) {
$where = "date >= '$fromDate%'";
} else if(!empty($toDate)) {
$where = "date <= '$toDate%'";
}
您可以这样做(我不太了解 PHP,因此可能需要更正检查日期值是否为空的语法):
//THIS IS THE SQL QUERY I USE FOR THE DATE RANGE FILTER
$where = '';
// check if both the dates are not null
if ($fromDate != null && $toDate != null) {
$where = "where date BETWEEN '$fromDate%' AND '$toDate%'";
} else if ($fromDate != null) {
$where = "where date >= '$fromDate%'";
} else if ($toDate != null) {
$where = "where date <= '$toDate%'";
}
$rs = mysql_query("select count(*) from table " . $where);