Untitled Document

Someone on the forum posed a question a short while ago asking how to create a more advanced search function using a + symbol as a separator. So I created this advanced search function. This search function is just slightly better than a normal search as it adds the ability to separate two keywords with a + symbol. Lets start with the search form.

form.cfm

<html>
<head>
<title>
Search Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

Insert two keywords separated by "+" sign.<br>
<form action="search.cfm" method="post" name="Search" id="Search">
<input name="keyword" type="text" id="keyword">
<input type="submit" name="Submit" value="GO">
</form>

</body>
</html>

The next item of business, is the actual search page. For this tutorial I will use a sample user search. In this search page there is three things happening. If the user only inputs one word into the field. It will search as normal. If the user adds two words separated by a + symbol, then we will break the two words into separate variables and lastly, search the database for those words.

search.cfm

<html>
<head>
<title>
Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<cfif isDefined ("form.keyword")>


<!--- // IF THE KEYWORD SUBMITTED DOES NOT CONTAIN + SIGN THEN JUST DO A BASIC QUERY // --->
<cfif form.keyword DOES NOT CONTAIN "+">
<cfquery name="qUsers" datasource="#dsn#">
SELECT *
FROM cf_users
WHERE login LIKE '%#form.keyword#%'
</cfquery>

<p>The name you choose was:</p>
<cfoutput query="qUsers">#qUsers.name#<br></cfoutput>

<!--- // IF THE KEYWORD DOES CONTAIN A + THEN BREAK THE KEYWORD INTO TWO VARIABLES
AND SEARCH USERS USING "LIKE" & "OR" FUNCTIONS // --->

<cfelse>
<cfset search_1 = trim(ListFirst(form.keyword, '+'))>
<cfset search_2 = trim(Listlast(form.keyword, '+'))>
<cfoutput>Search word 1 is: #search_1#</cfoutput><br>
<cfoutput>Search word 2 is: #search_2#</cfoutput>
<br><br>

<cfquery name="qUsers" datasource="#dsn#">
SELECT *
FROM cf_users
WHERE login LIKE '%#search_1#%' OR login LIKE '%#search_2#%'
</cfquery>

<cfoutput query="qUsers">#name#<br></cfoutput>

</cfif>
</cfif>
</body>
</html>

About This Tutorial
Author: Mark Aplet
Skill Level: Beginner 
 
 
 
Platforms Tested: CF5,CFMX
Total Views: 104,988
Submission Date: December 04, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 8
Discuss This Tutorial
  • Why not use

Advertisement

Sponsored By...
Mobile App Development (IOS, Android, Cordova, Phonegap, Objective-C, Java) - Austin, Texas Mobile Apps - Touch512, LLC.