We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Using SimpleSearch, works great, but I have need for more advanced searched that could use filters like publish date [older than, more recent than], parent [section] etc.
    I see you can set the ids in the snippet call, but not if that can be something integrated into the form itself.

    is there anyway to expand the simplesearch form to include filters?

    -thanks
    -sean
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 36575
      • 104 Posts
      Me too! Did you ever figure this out?
        • 26503
        • 620 Posts
        unfortunately - not yet .... but the deadline is Monday wink
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
        • I’m working too on these features...

          I tried with the postHooks option: let’s suppose that I manage to pass my "Published date" to the form and that date is "1304586360" (UNIX time).

          I have this call of SimpleSearch:

          <p>[[!SimpleSearch? &toPlaceholder=`sisea.results` &perPage=`20` &postHooks=`filter_results` &facetLimit=`5` &searchIndex=`what` &activeFacet=`filter_test` ]]</p>
          <h2>Search Results</h2>
          <p>[[+sisea.results]]</p> 


          And this is my snippet filter_test called by postHooks:

          <?php
          $c = $modx->newQuery('modResource');
          $c->where(array(
             'publishedon:<' => '1304586360',
          ));
          $c->select(array('id','pagetitle','alias','menutitle','publishedon'));
          $c->sortby('menuindex','ASC');
          
          $count = $modx->getCount('modResource',$c);
          
          $resources = $modx->getCollection('modResource',$c);
          
          $results = array();
          
          foreach ($resources as $resource) {
              $results[] = array(
                  'pagetitle' => $resource->get('pagetitle'),   
                  'id' => $resource->get('id'),    
                  'link' => $modx->makeUrl($resource->get('id')),    
                  'excerpt' => '',
              );
          }
          $hook->addFacet('filter_test',$results,$count);
          
          return true;
          


          But with this solution I get only the results filtered by date...and I loose my original search result!
          How can I combine the results altogether?

          Let me explain a little more:

          if I search for the word "habitat" I get 2 results,
          after I use the filter by date I get 15 results, but not the correct ones!


            TilliLab | MODX Ambassador
            website
            • 26503
            • 620 Posts
            Nope - I gave up on the filters and hooks and whatnot... just wasn’t doing what I needed... I wound up writing my own.

            It’s kinda messy, no pagination, but it works:


            The search snippett:
            <?php
            	if (empty($_POST)) return '';
            	if (!empty($submitVar) && empty($_POST[$submitVar])) return '';
            		
            	
            	$modx->setPlaceholder('name',$_POST["name"]);if($_POST["name"] == ''){$name = '';}else{$name = $_POST["name"];}
            	$modx->setPlaceholder('institution',$_POST["institution"]);if($_POST["institution"] == ''){$institution = '';}else{$institution = $_POST["institution"];}
            	$modx->setPlaceholder('academicinterest',$_POST["academicinterest"]);if($_POST["academicinterest"] == ''){$academicinterest = '';}else{$academicinterest = $_POST["academicinterest"];}
            	$modx->setPlaceholder('clinicalinterest',$_POST["clinicalinterest"]);if($_POST["clinicalinterest"] == ''){$clinicalinterest = '';}else{$clinicalinterest = $_POST["clinicalinterest"];}
            	$modx->setPlaceholder('province',$_POST["province"]);if($_POST["province"] == ''){$province = '';}else{$province = $_POST["province"];}
            	$modx->setPlaceholder('city',$_POST["city"]);if($_POST["city"] == ''){$city = '';}else{$city = $_POST["city"];}
            	
            
            	$tpl = $modx->getOption('tpl',$scriptProperties,'OncologistSearchResult');
            			
            	include_once $modx->config['base_path'].'core/components/oncologistSearch/include.oncologistSearch.php';
            	
            	//echo '<h6>' . $name . $institution . $academicinterest . '</h6>';
            	$searchResults = getOncologists($name,$institution,$academicinterest,$clinicalinterest,$province,$city);
            	
            	$output = '';
            	foreach($searchResults as $result){
            		$output .= $modx->getChunk($tpl,$result);
            		$count = $result["count"];
            	}
            	if(!$output){$count = "Sorry, no results were found.";}
            
            	$modx->setPlaceholder('resultSet',$output);
            	$modx->setPlaceholder('resultCount',$count);
            
            //return $output;
            
            return '';
            




            The actual search functions:
            <?php
            
            	$hostname = "******"; 
            	$username = "*****"; 
            	$password = "******"; 
            	$dbName = "********"; 
            	MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
            	@mysql_select_db( "$dbName") or die( "Unable to select database");
            	
            function getOncologists($name,$institution,$academicinterest,$clinicalinterest,$province,$city){
            			
            	$sql = "select * from custom_oncologist_database where id >= '1' AND ";
            	
            	if (!empty($name)){$sql .= "(firstname like '%" . $name . "%' OR lastname like '%" . $name . "%') AND ";}
            	if (!empty($institution)){$sql .= "(institution LIKE '%" . $institution . "%') AND ";}
            	if (!empty($province)){$sql .= "(province LIKE '%" . $province . "%') AND ";}
            	if (!empty($city)){$sql .= "(excity LIKE '%" . $city . "%') AND ";}
            	if (!empty($academicinterest)){$sql .= "(" . $academicinterest . " = '1') AND ";}
            	if (!empty($clinicalinterest)){$sql .= "(" . $clinicalinterest . " = '1') AND ";}
            		
            	$sql .= " oncologist_database = '1' order by lastname;";
            	
            	
            	$mysqlresults = mysql_query($sql) or die (mysql_error());
            	$records = mysql_num_rows($mysqlresults);
            	
            	if ($records == 0) {
            	    $count = "Sorry, no results were found.";
            	}else{
            		$count = $records . " Results";
            		}
            			
            	$i = 0;
            	$resultArray = '';
            	while ($row = mysql_fetch_assoc($mysqlresults)) {	
            						
            		$resultArray[$i]["id"] = $row["id"];
            		$resultArray[$i]["salutation"] = $row["salutation"];
            		$resultArray[$i]["firstname"] = $row["firstname"];
            		$resultArray[$i]["lastname"] = $row["lastname"];
            		$resultArray[$i]["resultCity"] = $row["excity"];
            		$resultArray[$i]["resultInstitution"] = $row["institution"];
            		$resultArray[$i]["resultProvince"] = $row["province"];
            		$resultArray[$i]["clinicalinterestspecifics"] = $row["clinical_interest_specifics"];;
            		$resultArray[$i]["clinicalinterestother"] = $row["other_clinical_interest"];
            		$resultArray[$i]["academicinterestspecifics"] = $row["academic_interest_specifics"];
            		$resultArray[$i]["academicinterestother"] = $row["other_academic_interest"];
            		   
            		$resultArray[$i]["count"] = $count;
            		$i++;
            	}
            	
            	//print_r($resultArray);
            	
            	return $resultArray;
            	
            }
            
            
            
            function getResultsDetail($id){
            			
            	$sql = "select * from custom_oncologist_database where id = '" . $id . "' LIMIT 1;";
            	
            	$mysqlresults = mysql_query($sql) or die (mysql_error());
            	$records = mysql_num_rows($mysqlresults);
            	if (mysql_num_rows($mysqlresults) == 0) {
            	    $count = "Sorry, no results were found.";
            	}else{
            		$count = $records . " Results";
            		}
            			
            	$i = 0;
            	$resultArray = '';
            	while ($row = mysql_fetch_assoc($mysqlresults)) {	
            		$resultArray[$i]["id"] = $row["id"];
            		$resultArray[$i]["salutation"] = $row["salutation"];
            		$resultArray[$i]["firstname"] = $row["firstname"];
            		$resultArray[$i]["lastname"] = $row["lastname"];
            		$resultArray[$i]["resultCity"] = $row["excity"];
            		$resultArray[$i]["resultInstitution"] = $row["institution"];
            		$resultArray[$i]["resultProvince"] = $row["province"];
            		$resultArray[$i]["clinicalinterestspecifics"] = $row["clinical_interest_specifics"];;
            		$resultArray[$i]["clinicalinterestother"] = $row["other_clinical_interest"];
            		$resultArray[$i]["academicinterestspecifics"] = $row["academic_interest_specifics"];
            		$resultArray[$i]["academicinterestother"] = $row["other_academic_interest"];
            		$resultArray[$i]["specialty"] = $row["specialty"];
            		$resultArray[$i]["specialty_other"] = $row["specialty_other"];
            		$resultArray[$i]["staff_or_resident"] = $row["staff_or_resident"];
            		$resultArray[$i]["excountry"] = $row["excountry"];
            		$resultArray[$i]["telephone"] = $row["telephone"];
            		$resultArray[$i]["extension"] = $row["extension"];
            		$resultArray[$i]["count"] = $count;
            		$i++;
            	}
            	
            	//print_r($resultArray);
            	
            	return $resultArray;
            	
            }
            
            




            the search result chunk:
            <li>
            	<label><a href="[[~351]]?oncologist=[[+id]]">[[+lastname]], [[+salutation]] [[+firstname]]</a></label>
            	<p>[[+resultInstitution]] - [[+resultCity]], [[+resultProvince]]<br />
            	Clinical Interests: [[+clinicalinterests]]<br />
            	Academic Interests: [[+academicinterests]]<br />	
            </li>
            




            that searches a custom table of users for specific criteria, while sloppy - it does work. I’m sure there are better ways to write most of the php - I’m just not a php guy....

            this one is far less errr. ’tidy’ but it searches the content tables for a bunch of different things. again - no pagination or ’relevance’

            
            <?php
            
            	$hostname = "xxxxxx"; 
            	$username = "xxxxxxx"; 
            	$password = "xxxxxxx"; 
            	$dbName = "xxxxxxxx"; 
            	MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
            	@mysql_select_db( "$dbName") or die( "Unable to select database");
            
            
            
            	//some default stuff
            	
            	$limit = $limit;
            	$offset = $offset;
            	
            	$tpl = $modx->getOption('tpl',$scriptProperties,'AdvancedSearchResult');
            	date_default_timezone_set('UTC');
            	
            	if(!isset($_POST["searchterms"])){ $searchterms = "";}else{$searchterms = $_POST["searchterms"];}
            	if(!isset($_POST["searchdefault"])){ $searchdefault = "";}
            	
            	$searchterms = trim(strip_tags($searchterms));
            	$searchdefault = $searchterms;
            
            	
            	if(isset($_POST["searchtitle"])){$searchtitle = 'checked';}else{$searchtitle = '';}
            	if(isset($_POST["searchbody"])){$searchbody = 'checked';}else{$searchbody = '';}
            	if(isset($_POST["phrase"])){$phrase ='checked';}else{$phrase = '';}
            	if(isset($_POST["andor"])){$andor ='and';}else{$andor = 'or';}	
            	if(!isset($_POST["fromdate"])){ $fromdate = "";}else{$fromdate =  trim($_POST["fromdate"]);}
            	if(!isset($_POST["todate"])){ $todate = "";}else{$todate = trim($_POST["todate"]);}
            
            //funtions 
            	function toEpoch($humandate,$offset){
            		$arrdates = explode('-',$humandate);
            		$epochdate = mktime(0,0,0,$arrdates[1],$arrdates[2],$arrdates[0]);
            		$epochdate = ($epochdate + $offset);
            		return $epochdate;
            	}
            
            	if (empty($_POST)) return '';
            	if (!empty($submitVar) && empty($_POST[$submitVar])) return '';
            	
            		if($phrase != 'checked'){
            			if(stristr($searchterms, ",")){$searchterms = str_replace(","," ",$searchterms);}
            			$searchterms = explode(" ", $searchterms);
            		}else{
            			$searchterms = array($searchterms);
            		}		
            		$sql = "";
            		$sql .= "select pagetitle, longtitle, id, description, introtext, content, publishedon from modx_site_content where (";
            					
            		if(($searchbody == 'checked') && ($searchtitle == '')){
            			foreach($searchterms as $i => $aterm){
            				if($i != 0){ $or = $andor;}else{$or = '';}
            				$sql .= $or . " content like '%" . $aterm . "%' ";
            			}	
            		}elseif(($searchbody == '') && ($searchtitle == 'checked')){
            			foreach($searchterms as $i => $aterm){
            				if($i != 0){ $or = $andor;}else{$or = '';}
            				$sql .= $or . " longtitle like '%" . $aterm . "%' ";
            			}
            		}else{
            			foreach($searchterms as $i => $aterm){
            				if($i != 0){ $or = $andor;}else{$or = '';}
            				$sql .= $or . " longtitle like '%" . $aterm . "%' or content like '%" . $aterm . "%' ";
            			}
            			
            		}
            		
            			$sql .= ") ";
            			if($fromdate != ""){  $sql .= " and publishedon >= '" . toEpoch($fromdate,0) . "'" ; }
            			if($todate != ""){  $sql .= " and publishedon <= '" . toEpoch($todate,86399) . "'" ; }
            			
            			$sql .= " and searchable = 1 and template = 1 order by publishedon;";
            			
            			$mysqlresults = mysql_query($sql) or die (mysql_error());
            			$records = mysql_num_rows($mysqlresults);
            		
            		$resultArray = '';
            		
            		
            		if (mysql_num_rows($mysqlresults) == 0) {
            		    $count = "Sorry, no results were found.";
            		}else{
            			$count = $records . " Results";
            			}
            		$i = 0;
            		while ($row = mysql_fetch_assoc($mysqlresults)) {
            			$pubdate = date("M/d/Y", $row["publishedon"]);
            			$strcontent = $row["description"];
            			$pubdate = $pubdate;
            			if($row["introtext"] == ''){$description = 'Sorry, no description is currently available.';}else{$description = $row["introtext"];}
            			if($row["longtitle"] == ''){$longtitle = $row["pagetitle"];}else{$longtitle = $row["longtitle"];}			
            			$resultArray[$i]["id"] = $row["id"];
            			$resultArray[$i]["longtitle"] = $longtitle;
            			$resultArray[$i]["pubdate"] = $pubdate;
            			$resultArray[$i]["description"] = $description;
            			$resultArray[$i]["records"] = $records;
            			$i++;
            		}
            
            
            	$output = '';
            	foreach($resultArray as $result){
            	 $output .= $modx->getChunk($tpl,$result);
            	}
            	
            		if($andor == "and"){$phandor = 'checked';}else{$phandor = '';}
            		$modx->setPlaceholder('searchdefault',$searchdefault);
            		$modx->setPlaceholder('searchtitle',$searchtitle);
            		$modx->setPlaceholder('searchbody',$searchbody);
            		$modx->setPlaceholder('phrase',$phrase);
            		$modx->setPlaceholder('fromdate',$fromdate);
            		$modx->setPlaceholder('todate',$todate);
            		$modx->setPlaceholder('andor',$phandor);
            	
            		$modx->setPlaceholder('count',$count);
            	
            		$modx->setPlaceholder('searchresults',$output);
            	
            	
            	
            	
            	return '';
            
            



            and the snippet that posts to it....
            <p>[[AdvancedSearch? &submitVar=`execute-search` &limit=`10` &offset=`0`]]</p>
            <form class="form advanced-search" action="[[~[[*id]]]]" method="post" name="advanced-search">
            <ul>
            <li><label>Search term[s]:</label><input class="searchbox" style="width: 600px;" type="text" name="searchterms" value="[[+searchdefault]]" /></li>
            <li><label>Search in title</label><input class="checkbox" type="checkbox" name="searchtitle" /></li>
            <li><label>Search in body</label><input class="checkbox" type="checkbox" name="searchbody" /></li>
            <li><label>Search as a phrase</label><input class="checkbox" type="checkbox" name="phrase" /></li>
            <li><label>Must contain all terms</label><input class="checkbox" type="checkbox" name="andor" /></li>
            <li><label class="datefield">From (published) date:</label><input class="datefield" type="text" name="fromdate" value="[[+fromdate]]" /> <input class="submit date" onclick="displayDatePicker('fromdate', this);" type="button" value="Set Date" /></li>
            <li><label class="datefield">To (published) date:</label><input class="datefield" type="text" name="todate" value="[[+todate]]" /> <input class="submit date" onclick="displayDatePicker('todate', this);" type="button" value="Set Date" /></li>
            <li><label> </label><input class="submit advanced" type="submit" name="execute-search" value="submit" /></li>
            </ul>
            </form><!--
            [ [!getPage? &element=`AdvancedSearch`] ]
            [ [!getPage? &element=`searchresults`] ]
            -->
            <ul class="advanced-results">
            <li>
            <h4>[[+count]]</h4>
            </li>
            [[+searchresults]]</ul>
            
            







            So - [Again!] no pagination, php sexiness is weak, I should at least clean up the db connection stuff, oh yea - & no real validation. but if you can use any of this & bend it to your will...... have at it - and if you figure out the pagination... hit me back! smiley

            -good luck
            -sean






              *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

              Sean Kimball CLP, CLS.
              Technical Director / Sr. Developer | BigBlock Studios
              ._______________________________________________.
              Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
              27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
              phone/fax: 905-426-5525
            • Thanks for sharing!

              If you don’t have too many results you could even use a jquery pagination, like this:

              http://www.egrappler.com/jquery-pagination-plugin-smart-paginator/

              If you want a more performing result you may use the mysql "limit" in the query and call it back with url parameter
                TilliLab | MODX Ambassador
                website
                • 26503
                • 620 Posts
                Hi Pandro;

                yea - they could have up to a couple hundred results [and I’m not a big fan of using javascript if it’s not necessary] and I just haven’t had the time to to the LIMIT thing....

                -sean
                  *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                  Sean Kimball CLP, CLS.
                  Technical Director / Sr. Developer | BigBlock Studios
                  ._______________________________________________.
                  Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                  27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                  phone/fax: 905-426-5525