Filtering search results by date created range.

4 posts by 2 authors in: Forums > CMS Builder
Last Post: January 3, 2014   (RSS)

By gkornbluth - November 25, 2013 - edited: November 25, 2013



Hi All,
I’ve created a search form that filters results and displays them on a separate list page using the _match, _max and _min functions.

I’m stuck on how to filter the results to show only those records created in the last month, or the last 2 months, or the last 3 months, or more than 3 months old, in addition to any other filters imposed.

Here’s an example of the code I’m using. The last row in the table is the one I’m stuck on.

 <table width="40%" border="0" cellspacing="0" cellpadding="2">
<form method="POST" class="arial_14" action="listings.php">
      <tr>
        <td class="arial_14"><font color="#FFFFFF"><b>Price: </b></font> </td>
        <td><select name = "price_min"  width="135" class="arial_14" style="width: 135px">
            <option value="">Minimum</option>
            <option value="">Any</option>
            <option value="">$0</option>
            <option>$  500</option>
            <option>$1,000</option>
            <option>$1,500</option>
            <option>$2,000</option>
          </select></td>
        
       <td>&nbsp; &nbsp; &nbsp;</td>
        <td><select name = "price_max" width="135" class="arial_14" style="width: 135px">
            <option value="">Maximum</option>
            <option value="">Unlimited</option>
            <option>$  500</option>
            <option>$1,000</option>
            <option>$1,500</option>
            <option>$2,000</option>
           </select></td>
      </tr>
     <tr>
        <td class="arial_14"><font color="#FFFFFF"><b>Breed: </b></font></td>
        <td colspan="3"><select name = "dog_breed_match" width="300" class="arial_14" style="width: 300px">
            <option value="">Please Choose a Breed</option>
            <?php foreach (getListOptions('lm_listing', 'dog_breed') as $value => $label2): ?>
            <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['dog_breed']);?>> <?php echo $label2; ?></option>
            <?php endforeach ?>
          </select></td>
      </tr>
       
<tr>
 <td class="arial_14"><font color="#FFFFFF"><b>Date Listed: </b></font></td>
  <td colspan="3"><select name="age" width="300" class="arial_14" style="width: 300px">
            <option value="">All Dates</option>
            <option value="a">Listed less than 1 Month ago</option>
            <option value="b">Listed less than 2 Months ago</option>
            <option value="c">Listed less than 3 Months ago</option>
            <option value="d">Listed more than 3 Months ago</option>
          </select></td>
      </tr>  
      <tr>
      <td class="arial_14"><b>&nbsp;</b> </td>
        <td align="left" colspan="3"><input type="submit" name="submit" value="Search" ></td>
      </tr>
     </form>
      <FORM ACTION="" "class=arial_14">
      <tr>
      <td class="arial_14"><b>&nbsp;</b> </td>
        <td align="left" colspan="3"><INPUT TYPE="submit" VALUE="Cancel Search Filters"></td>
      </tr>
      </FORM>
    </table>
   

Has anybody done this type of thing before that’s willing to share their knowledge?

Thanks

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Dave - November 28, 2013

Hi Jerry, 

Try this:

<select name="age_min" width="300" class="arial_14" style="width: 300px">
  <option value="">All Dates</option>
  <option value="<?php echo mysql_datetime(strtotime("-1 month")); ?>">Listed less than 1 Month ago</option>
  <option value="<?php echo mysql_datetime(strtotime("-2 month")); ?>">Listed less than 2 Months ago</option>
  <option value="<?php echo mysql_datetime(strtotime("-3 month")); ?>">Listed less than 3 Months ago</option>
</select>

Let me know if that works for you.

Dave Edis - Senior Developer
interactivetools.com

By gkornbluth - January 3, 2014 - edited: January 3, 2014

Sorry that it took so long to post, but here's the code that finally worked:

On the search page (search.php)

<table width="40%" border="0" cellspacing="0" cellpadding="2">
    <form method="POST" class="your_class" action="listings-s.php">
  <tr>
        <td class="arial_14"><font color="#FFFFFF"><b>Date Listed: </b></font></td>
        <td colspan="3">
            <select name="months_ago" width="300" class="your_class" style="width: 300px">
  <option value="">All Dates</option>
  <option value="1">Listed less than 1 Month ago</option>
  <option value="2">Listed less than 2 Months ago</option>
  <option value="3">Listed less than 3 Months ago</option>
</select>
</td>
 </tr>  
<tr>
<td class="your_class"><b>&nbsp;</b> </td>
<td align="left" colspan="3"><input type="submit" name="submit" value="Search" ></td>
</tr>
</form>
</table>


And on the results page (listings-s.php)

<?php
if (@$_REQUEST['months_ago']) {
  $monthsAgo = $_REQUEST['months_ago'];
  $_REQUEST['createdDate_min'] = mysql_datetime(strtotime("-$monthsAgo month"));
}
 ?>

<?php
  $searchCriteria = '';
  if (@$_REQUEST['months_ago']) { $searchCriteria .= "Listed Less Than: {$_REQUEST['months_ago']} months ago - "; }
  $searchCriteria = chop($searchCriteria, ', '); // remove trailing , or spaces
    $searchCriteria = chop($searchCriteria, '- '); // remove trailing - or spaces
  ?>

<div align="center"> <span class="your_class"> There are <?php echo $listingMetaData['totalRecords']; ?> listings that match your search for:
 <?php if ( @!$searchCriteria ):?>
 All Listings
 <?php else :?>
 <br />
 <?php echo $searchCriteria ?>
 <?php endif ?>
</span>
              
 <a href="search.php"><span class="your_class"><br />
 &lt;&lt; <u>Return to the Advanced Search Page</u></span></a></div>

Hope that helps someone.

Jerry Kornbluth
 

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php