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)

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

Hi Dave,

I've managed to get the search criteria and the teltale on the results page working and my code is below for anyone who can use it, but there's one more issue that's popped up.

If the price field is empty it throws the  _max searches off because it includes all of the blank fields and I'm not sure how to add the crriteria && $!record ['price'] = "" to exclude empty fields from the search, from the teltale tallies on the results page, and from the per page counters on the results page (not shown in the code) .

Any pointers?

Thanks,

Jerry Kornbluth

________________________________________________________________________________

Existing Code - Search page:

Body:

<table width="40%" border="0" cellspacing="0" cellpadding="2">
    <form method="POST" class="arial_14" action="listings-s.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>
            <option>$4,000</option>
            <option>$6,000</option>
            <option>$8,000</option>
            <option>$10,000</option>
            <option>$15,000</option>
            <option>$20,000</option>
            <option>$25,000</option>
            <option>$30,000</option>
            <option>$35,000</option>
            <option>$40,000</option>
            <option>$45,000</option>
            <option>$50,000</option>
            <option>$55,000</option>
            <option>$60,000</option>
            <option>$65,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>
            <option>$4,000</option>
            <option>$6,000</option>
            <option>$8,000</option>
            <option>$10,000</option>
            <option>$15,000</option>
            <option>$20,000</option>
            <option>$25,000</option>
            <option>$30,000</option>
            <option>$35,000</option>
            <option>$40,000</option>
            <option>$45,000</option>
            <option>$50,000</option>
            <option>$55,000</option>
            <option>$60,000</option>
            <option>$65,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>Category: </b></font></td>
        <td colspan="3"><select name = "category_match" width="300" class="arial_14" style="width: 300px">
            <option value="">All Listings</option>
            <?php foreach (getListOptions('lm_listing', 'category') as $value => $label): ?>
            <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['category']);?>> <?php echo $label; ?></option>
            <?php endforeach ?>
          </select></td>
      </tr>
      <tr>
        <td class="arial_14"><font color="#FFFFFF"><b>Sex: </b></font></td>
        <td colspan="3"><select name = "sex_match" width="300" class="arial_14" style="width: 300px">
            <option value="">Either</option>
            <?php foreach (getListOptions('lm_listing', 'sex') as $value => $label3): ?>
            <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['sex']);?>> <?php echo $label3; ?></option>
            <?php endforeach ?>
          </select></td>
      </tr>
       <tr>
        <td class="arial_14"><font color="#FFFFFF"><b>Guarantee: </b></font></td>
        <td colspan="3"><select name = "guarantees_match" width="300" class="arial_14" style="width: 300px">
            <option value="">Any</option>
            <?php foreach (getListOptions('lm_listing', 'guarantees') as $value => $label5): ?>
            <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['guarantees']);?>> <?php echo $label5; ?></option>
            <?php endforeach ?>
          </select></td>
      </tr>

      <tr>
        <td class="arial_14" ><font color="#FFFFFF"><b>State/County: </b></font></td>
        <td colspan="3"><select name = "state_county_match" width="300" class="arial_14" style="width: 300px">
              <option value="">Any</option>
            <?php foreach (getListOptions('lm_listing', 'state_county') as $value => $label6): ?>
            <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['state_county']);?>> <?php echo $label6; ?></option>
            <?php endforeach ?>
          </select>
          </td>
      </tr>
      
       <tr>
        <td class="arial_14"><font color="#FFFFFF"><b>Country: </b></font></td>
        <td colspan="3"><select name = "country_match" width="300" class="arial_14" style="width: 300px">
            <option value="">Any</option>
            <?php foreach (getListOptions('lm_listing', 'country') as $value => $label7): ?>
            <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['country']);?>> <?php echo $label7; ?></option>
            <?php endforeach ?>
          </select></td>
      </tr>
     
      <tr>
        <td class="arial_14"><font color="#FFFFFF"><b>Show Listings: </b></font></td>
        <td colspan="3"><select name = "status_match"width="300" class="arial_14" style="width: 300px" >
            <option value="">All Listings</option>
            <?php foreach (getListOptions('lm_listing', 'status') as $value => $label4): ?>
            <option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['status']);?>> <?php echo $label4; ?></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="months_ago" width="300" class="arial_14" 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="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>

Results Page Head (to sort out the number of months to show)

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

  Results Page - Telltale (Body)

   <?php
  $searchCriteria = '';
  if (@$_REQUEST['price_min'])       { $searchCriteria .= "Minimum price: {$_REQUEST['price_min']} - "; }
  if (@$_REQUEST['price_max'])       { $searchCriteria .= "Maximum price: {$_REQUEST['price_max']} - "; }
  if (@$_REQUEST['dog_breed_match']) { $searchCriteria .= "Breed: {$_REQUEST['dog_breed_match']} - "; }
  if (@$_REQUEST['category_match']) { $searchCriteria .= "Category: {$_REQUEST['category_match']} - "; }
 if (@$_REQUEST['sex_match']) { $searchCriteria .= "Sex: {$_REQUEST['sex_match']} - "; }
  if (@$_REQUEST['state_county_match']) { $searchCriteria .= "Guarantee: {$_REQUEST['state_county_match']} - "; }
  if (@$_REQUEST['country_match']) { $searchCriteria .= "Category: {$_REQUEST['country_match']} - "; }
  if (@$_REQUEST['status_match']) { $searchCriteria .= "Status: {$_REQUEST['status_match']} - "; }
// if (@$_REQUEST['createdDate_min']) { $searchCriteria .= "Age: {$_REQUEST['createdDate_min']} - "; }
  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
  // $searchCriteria = preg_replace("/(.+),/", "$1, and", $searchCriteria); // replace last , with ", and"
 // if ($searchCriteria) { print "Results for the search:<br> $searchCriteria<br/>\n"; }
?>
              </div>
              <div align="center"> <span class="arial_14"> There are <?php echo $lm_listingMetaData['totalRecords']; ?> listings that match your search for:
                <?php if ( @!$searchCriteria ):?>
                All Listings
                <?php else :?>
                <br />
                <?php echo $searchCriteria ?>
                <?php endif ?>
                </span>

And the results themselves,

 <?php foreach ($lm_listingRecords as $record): ?>

 <?php if($record['field_1']):?>
                                  <b>Field 1:</b> <?php echo $record['field_1']?><br />
                                  <?php endif ?>


 <?php if($record['field_2']):?>
                                  <b>Field 2:</b> <?php echo $record['field_2']?><br />
                                  <?php endif ?>


 <?php endforeach ?>

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