Displaying the search criteria submitted in a search form on a search results list page

11 posts by 2 authors in: Forums > CMS Builder
Last Post: December 31, 2013   (RSS)

Hi All,

I don’t think I missed it on the forum or in the search documentation, or possibly my brain is just asleep.

I’m looking for an easy way to display the search criteria submitted in a search form on a search results list page.

So, in the case of the code below, if the user submitted values of $500 for price_min, $1500 for price_max, and Rottweilers for the dog_breed_match, I want to display something like this on the search results list page:

"Here are your search results listings with a Minimum Price of $500, a maximum price of $1500 and a Breed of Rottweiler."

If they didn’t enter a value in a particular search field, I’d like it not to appear in the display, but I think that should be pretty straight forward if statements.

Thanks for your help,

Jerry Kornbluth

<form method="POST" class="arial_14" action="listings-s.php">
      <table>
<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>
             </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>
                    </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"><b>&nbsp;</b> </td>
        <td align="left" colspan="3"><input type="submit" name="submit" value="Search" ></td>
      </tr>
</table>
     </form>
 

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 something like this:

<?php
  $searchTermsEnglish = '';
  if (@$_REQUEST['price_min'])       { $searchTermsEnglish .= "a minimum price of {$_REQUEST['price_min']}, "; }
  if (@$_REQUEST['price_max'])       { $searchTermsEnglish .= "a maximum price of {$_REQUEST['price_max']}, "; }
  if (@$_REQUEST['dog_breed_match']) { $searchTermsEnglish .= "a breed of {$_REQUEST['dog_breed_match']}, "; }
  $searchTermsEnglish = chop($searchTermsEnglish, ', '); // remove trailing , or spaces
  $searchTermsEnglish = preg_replace("/(.+),/", "$1, and", $searchTermsEnglish); // replace last , with ", and"
  if ($searchTermsEnglish) { print "Here are your search results listings for: $searchTermsEnglish<br/>\n"; }
?>

Getting rid of the trailing comma is always a little tricky (I'm using chop). You either put everything in an array and join on comma or add the comma and then remove it.  I thought this might be simpler.  Also I used a regular expression to replace the last comma with ", and" (which can be changed to " and" if you don't like Oxford Commas.  ;) 

Let me know if that works for you.

Dave Edis - Senior Developer
interactivetools.com

Thanks Dave, it works (almost) perfectly...

One of my search  fields is a MySQL query

  <select name="createdDate_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>

and: if (@$_REQUEST['createdDate_min']) { $searchCriteria .= "Age: {$_REQUEST['createdDate_min']} - "; }

returns: 2013-10-29 14:28:15

I'm not sure how to reformat that to "less than one month ago" (the original label of the search option)

Any suggestions?

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 - December 2, 2013

Hi Jerry, 

There's no really easy way to do that.  We could calculate the number of seconds in a month and do some math, but it will get complicated pretty quick.

An easier way might be to just pass around the number of months and then fake the request field that CMSB's search is looking for.

Try this:

  • Rename your "createdDate_min" field to "months_ago"
  • Replace <?php echo mysql_datetime(strtotime("-1 month")); ?> with 1 (and 2 with 2, etc);
  • Add this line of code to the top of your script BEFORE getRecords();

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

Basically this just makes PHP think we submitted a field called createdDate_min, even though we didn't.  

Then you can output the months ago value like this:

if (@$_REQUEST['months_ago']) { $searchCriteria .= "Age: {$_REQUEST['months_ago']} months ago - "; }

Let me know if that works for you. Thanks!

Dave Edis - Senior Developer
interactivetools.com

Thanks Dave,

It's a little late here but tomorrow I'll try to understand what you suggested.

Best as always,

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 gkornbluth - December 8, 2013 - edited: December 8, 2013

Hi Dave,

Sorry for the delay,

I implemented your suggestion this morning, and it passes the months_ago value ti the listing page as expected, but does not seem to pass the createdDate_min value and I'm not sure why.

Any suggestions?

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

listings-s4.php 22K

search4.php 17K

By Dave - December 8, 2013

Hi Jerry, 

It fakes the submission of a createDate_min field, but all the getRecords() viewers on search.php have allowSearch set to false, so the built in CMS search features are being ignored.

Try setting allowSearch to true for the section you want to filter (lm_listing)? 

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

Hi Dave,

Thanks for looking a this again.

I tried changing the allowSearch to true, but that didn’t seem to help.

The original code (below) filters the results, even with the allowSearch set to false.
 
When I implement the “fake” code, the correct value (1 month ago, etc) appears in the result page telltale, but the createdDate search is ignored and all records are returned.

Best,

Jerry   

<select name="createdDate_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>

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 - December 12, 2013

Hi Jerry, 

You could try setting 'debugSql' => true, to see what's happening, or feel free to email it over to me and I can take a look for you.  Thanks.

Dave Edis - Senior Developer
interactivetools.com