Search By Date
3 posts by 2 authors in: Forums > CMS Builder
Last Post: October 26, 2009 (RSS)
By Kenny - October 24, 2009
I have a date searching issue.
I have the form set up, but it it is escaping me right now how to get it to work correctly.
The problem is that I have a Month, Year, and Day drop-down set up but I need them to talk to each other. Right now if you search for January 9, 2009 (a day with NO entries) it will bring up all the entries for September 9th and October 9th.
Here's my code:
I have the form set up, but it it is escaping me right now how to get it to work correctly.
The problem is that I have a Month, Year, and Day drop-down set up but I need them to talk to each other. Right now if you search for January 9, 2009 (a day with NO entries) it will bring up all the entries for September 9th and October 9th.
Here's my code:
<form action="maintenance.php" method="get">
<div align="center">
<table id="table4" border="0" cellpadding="3" cellspacing="3">
<tbody>
<tr>
<td bgcolor="#FFFFFF">
<table id="table5" border="0" cellpadding="3" cellspacing="3">
<tbody>
<tr>
<td style="font-size: 10pt">Search By Date</td>
<td>
<select name="createdDate_year">
<option value="01" >January</option>
<option value="02" >February</option>
<option value="03" >March</option>
<option value="04" >April</option>
<option value="05" >May</option>
<option value="06" >June</option>
<option value="07" >July</option>
<option value="08" >August</option>
<option value="09" >September</option>
<option value="10" >October</option>
<option value="11" >November</option>
<option value="12" >December</option>
</select>
<select name="createdDate_day">
<option selected value="01" >1</option>
<option value="02" >2</option>
<option value="03" >3</option>
<option value="04" >4</option>
<option value="05" >5</option>
<option value="06" >6</option>
<option value="07" >7</option>
<option value="08" >8</option>
<option value="09" >9</option>
<option value="10" >10</option>
<option value="11" >11</option>
<option value="12" >12</option>
<option value="13" >13</option>
<option value="14" >14</option>
<option value="15" >15</option>
<option value="16" >16</option>
<option value="17" >17</option>
<option value="18" >18</option>
<option value="19" >19</option>
<option value="20" >20</option>
<option value="21" >21</option>
<option value="22" >22</option>
<option value="23" >23</option>
<option value="24" >24</option>
<option value="25" >25</option>
<option value="26" >26</option>
<option value="27" >27</option>
<option value="28" >28</option>
<option value="29" >29</option>
<option value="30" >30</option>
<option value="31" >31</option>
</select>
<select name="createdDate_year">
<option value="2009" selected="selected" >2009</option>
</select> </td>
</tr>
</tbody>
</table>
<p>
</p>
<div align="center">
<input name="search" type="submit" value=" Search By Date " />
</div>
</td>
</tr>
</tbody>
</table>
</div>
</form>
Re: [sagentic] Search By Date
By Dave - October 26, 2009
Hi Kenny,
I think you may have a typo in your month fieldname. Try renaming it to <select name="createdDate_month">
I think that will do it. You can see what SQL is being generated by adding:
'debugSql' => true,
If you want to do something more advanced, like date range searches between multiple dates, you need to combine the 3 data fields into a date in PHP and then pass it to MySQL with a WHERE.
I sometimes use this function to create date fields:
Then I can display date fields like this:
And combine them like this:
And then:
'where' => $where,
The trick when doing date queries with mySQL directly is to make sure the date looks like this: YYYY-MM-DD HH:MM:SS
So those are some other ways to do it as well. Hopefully one of those solutions will work for you. Let me know how it goes.
I think you may have a typo in your month fieldname. Try renaming it to <select name="createdDate_month">
I think that will do it. You can see what SQL is being generated by adding:
'debugSql' => true,
If you want to do something more advanced, like date range searches between multiple dates, you need to combine the 3 data fields into a date in PHP and then pass it to MySQL with a WHERE.
I sometimes use this function to create date fields:
//
function showDatePulldown($fieldname) {
// show fields
print "<select name='{$fieldname}:day' style='width: auto'>\n";
print "<option value=''>day</option>\n";
foreach (range(1,31) as $dayNum) {
$selectedAttr = selectedIf(@$_REQUEST["{$fieldname}:day"], $dayNum, true);
echo "<option value='$dayNum' $selectedAttr>$dayNum</option>\n";
}
print "</select>\n";
print "<select name='{$fieldname}:mon' style='width: auto'>\n";
print "<option value=''>mon</option>\n";
foreach (range(1,12) as $monthNum) {
$monthName = date('M', mktime(0,0,0,$monthNum));
$selectedAttr = selectedIf(@$_REQUEST["{$fieldname}:mon"], $monthNum, true);
echo "<option value='$monthNum' $selectedAttr>$monthName</option>\n";
}
print "</select>\n";
print "<select name='{$fieldname}:year' style='width: auto'>\n";
print "<option value=''>year</option>\n";
foreach (range(date('Y')-1, date('Y')+5) as $year) {
$selectedAttr = selectedIf(@$_REQUEST["{$fieldname}:year"], $year, true);
echo "<option value='$year' $selectedAttr>$year</option>\n";
}
print "</select>\n";
?>
Then I can display date fields like this:
showDatePulldown('startDate');
showDatePulldown('endDate');
And combine them like this:
$startDate = mysql_real_escape_string( sprintf('%04d-%02d-%02d 00:00:00', $_REQUEST['startDate:year'], $_REQUEST['startDate:mon'], $_REQUEST['startDate:day']) );
$endDate = mysql_real_escape_string( sprintf('%04d-%02d-%02d 23:59:59', $_REQUEST['endDate:year'], $_REQUEST['endDate:mon'], $_REQUEST['endDate:day']) );
$where = "('$startDate' <= createdDate AND createdDate <= '$endDate')";
And then:
'where' => $where,
The trick when doing date queries with mySQL directly is to make sure the date looks like this: YYYY-MM-DD HH:MM:SS
So those are some other ways to do it as well. Hopefully one of those solutions will work for you. Let me know how it goes.
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com
Re: [Dave] Search By Date
By Kenny - October 26, 2009
Well that was easy - I shouldn't be coding so late at night! But when else would we do it?
I like the date range search - I'll give that a try. Thanks Dave!
I like the date range search - I'll give that a try. Thanks Dave!