By: Ayhan

Have you even wanted to search for multiple words in a search (but as separate elements)? If a user typed in:

"ColdFusion MX Tutorials"

in a search box, this tutorial will show you how to accuretly return results from your data, where each individual word is filtered from the data...

<CFQUERY NAME="searchresults" DATASOURCE="yourdatasource">
    SELECT *
    FROM table
    WHERE 0=0
    AND

(
<cfloop index="i" list="#form.searchkeyword#" delimiters=" ">
    <cfoutput>
fieldname LIKE '%#i#%' AND </cfoutput>
</cfloop>
'%%')

</CFQUERY>

it is so simple ......

About This Tutorial
Author: Ayhan
Skill Level: Advanced 
 
 
 
Platforms Tested: CFMX
Total Views: 27,095
Submission Date: April 05, 2004
Last Update Date: June 05, 2009
All Tutorials By This Autor: 4
Discuss This Tutorial
  • this tutorial is too good, man it gave me the exact outcome i wanted. thanks dude

  • How could I create say 3 list menu´s that search different sections eg. price, location and style in the same product. I have tryed to find the info on this and can only find info on single text field search´s.

  • Helped reduce about 20 lines of code!! Thanks

  • What if you wanted to search on all the words individually? So it was OR instead of And. I tried this and it displayed all the records not instead of the words i inputted.

  • For Kiwi, how to handle multiple fields. SELECT * FROM tablename WHERE (0=0 AND (column1 LIKE '%#thisword#%' OR column2 LIKE '%#thisword#%' OR column3 LIKE '%#thisword#%') ) The cfoutput tags used in the example by Ayhan aren't necessary. Nor is the structure of the last AND with the '%%') at the very end.

  • how would you go about using more than one fieldname in this query? e.g. you have 4 fields you wanted to search for the keywords in.

Advertisement

Sponsored By...
Powered By...