Searching by Date
2 posts by 2 authors in: Forums > CMS Builder
Last Post: December 19, 2014 (RSS)
I need to create a Search Box to allow users to filter listings by inputting a date field and getting the results listed.
Example: Search Listings from 1st December, 2014 to 15th December, 2014.
The Date Field will be as a drop down menu.
Thank you.
By gkornbluth - December 19, 2014 - edited: December 19, 2014
Hi
Assuming that you want to compare to the created_date for the records in your section, you might try a where statement something like:
'where' => " createdDate < '$max_date' AND createdDate > '$min_date' ",
This won’t work if you want to take the updated date of records into account but it’s a start.
This recipe from my CMSB Cookbook http://www.thecmsbcookbook.com might give you some ideas on how to deal with submitting the search parameters safely:
ALLOWING VISITOR TO SET WHERE VALUES
In this example you're trying to limit the records shown to those that match the
value of a field called project_title, so first you'll need to create a list field called project_title in your table.
Then, we'll assume that in any record the values for that field can be either Test event 1 or Test event 2.
At the top of your page in the load records calls use the code:
_____ code ________________________________________________
$where = "";
if (@$FORM['where'] == 'a') { $where = 'Test Event 1'; }
if (@$FORM['where'] == 'b') { $where = 'Test Event 2'; }
list($$your_tableRecords, $$your_tableMetaData) = getRecords(array(
'tableName' => '$your_table',
'where' => " project_title = '$where' ",
));
__________________________________________________________
And in the body, the form code would be:
_____ code ________________________________________________
<form method="POST" action="<?php echo
$_SERVER['PHP_SELF'] ?>">
<select name="where">
<option value="">Select</option>
<option value="a">Event 1</option>
<option value="b">Event 2</option>
</select>
<input type="submit" name="submit" value="Choose an exhibition to View">
</form>
__________________________________________________________
NOTE: According to Dave Edis from Interactive Tools, the reason to do it that way by passing a letter (or word or code,
it doesn't matter) and testing for that instead of just specifying the order by in the option value directly is because
you don't want users to be able to pass MySQL directly into your program or it's a security risk.
You can expand this idea to create as complex a set of criteria s required.
ANOTHER NOTE: Jason Sauchuk offered this mini tutorial on the use of single and double quotes. He said:
In PHP, a string that is set with double quotes can have variables inserted directly into it without concatenation.
example:
_____ code ________________________________________________
<?php
$myName = "Jason";
$greeting = "Hello, my name is $myName";
?>
__________________________________________________________
The value of $greeting would be:
Hello, my name is Jason
If we took this same piece of code and used single quotes:
_____ code ________________________________________________
<?php
$myName = "Jason";
$greeting = 'Hello, my name is $myName';
?>
__________________________________________________________
The value of $greeting would be:
Hello, my name is $myName
PHP will not put the value of $myName into the string.
You also might try a search on the forum for "search by date range" or a Google search for php mysql search by date range, and see what you get
Hope this helps a little,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php