944,145 Members | Top Members by Rank

Ad:
  • ColdFusion Discussion Thread
  • Unsolved
  • Views: 3901
  • ColdFusion RSS
May 22nd, 2009
0

Coldfusion search multiple words.

Expand Post »
Hi,

I would like to search one field in database with multiple words. Also i will allow searches for example : a or cs....(one,two characters).
My code:

ColdFusion Syntax (Toggle Plain Text)
  1. <cfparam name="URL.NAME" default="1" type="Any">
  2. <cfquery name="search" datasource="datasource">
  3. SELECT *
  4. FROM search
  5. WHERE NAME LIKE ="%#URL.NAME#%"
  6. ORDER BY DATE DESC
  7. </cfquery>

this code works fine but only with one word or if there is exact match with multiple words.
Example:
database rows:
1.adobe photohop
2.adobe photoshop cs4

search string adobe(my code) will display both results,but
search string adobe cs4 will not display results. I want if user type one,two, three words to match any of these words and to display all results like google.
What am I doing wrong? I also tried to replace characters and add + but no results.
Any idea or example i would appreciate.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pirula is offline Offline
11 posts
since Apr 2009
May 27th, 2009
0

Re: Coldfusion search multiple words.

You don't explain enough about what the data looks like and the result you are trying to get. But couldn't you break the url.name into pieces based on spaces and then select LIKE for each piece?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gdmprog1 is offline Offline
3 posts
since May 2009
May 27th, 2009
0

Re: Coldfusion search multiple words.

i was tired to think when i posted question. You can use and i am using cfif and cfloop tags in cfqueries. Just loop url.name, that is simple explanation and for advanced one you will need a couple of cfif,cfset and cfloop's in cfqueries.
Last edited by pirula; May 27th, 2009 at 11:23 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pirula is offline Offline
11 posts
since Apr 2009
Oct 30th, 2009
0
Re: Coldfusion search multiple words.
you have to break your input into 2 elements separated by a delimiter(I did it with a space) and put them into an array

coldfusion Syntax (Toggle Plain Text)
  1. <cfparam name="strSearch" default="">
  2. <cfparam name="myArrayList" default="">
  3. <cfset myArrayList=ArrayNew(1)>
  4.  
  5. <cfif isdefined("form.searchMeals") AND form.searchMeals neq "">
  6. <cfset strSearch = #trim(form.searchMeals)#>
  7. <cfset strSearch = #lcase(strSearch)#>
  8. <cfif strSearch contains "">
  9. <cfset strSearch = Replace(strSearch," ",",")>
  10. <cfset myArrayList = ListToArray(strSearch)>
  11. <cfif myArrayList[1] neq "">
  12. <cfset element1 = #myArrayList[1]#>
  13. </cfif>
  14. <cfif strSearch contains ",">
  15. <cfset element2 = #myArrayList[2]#>
  16. </cfif>
  17. </cfif>
  18. </cfif>

now you can easily pass both or either one into your query.
Hope this helps.
Last edited by peter_budo; Oct 30th, 2009 at 7:17 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kg_ally is offline Offline
9 posts
since Mar 2009
Feb 11th, 2010
0
Re: Coldfusion search multiple words.
You can try to find some Capitalization method from the database that you are using and search it like.
In postgreSQL they have initcap() method for capitalizing the first letters of the words. I don't really know but I did not find any method in coldfusion that converts the first letter of a set of words in uppercase. so that's how I did manage it.

ColdFusion Syntax (Toggle Plain Text)
  1. and initcap(column_name) LIKE initcap('%#url.name#%')

Hope that helps.
Last edited by grungy coder; Feb 11th, 2010 at 7:30 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grungy coder is offline Offline
7 posts
since Feb 2010
Feb 11th, 2010
0
Re: Coldfusion search multiple words.
Word order I assume counts? (For example "Adobe ColdFusion" vs "ColdFusion Adobe.") Also, you may want to use <cfqueryparam> to help against SQL Injections.

ColdFusion Syntax (Toggle Plain Text)
  1. <cfset searchString = "Immigration Law" />
  2.  
  3. <cfquery name="Info" datasource="YourDB">
  4. SELECT *
  5. FROM Areas
  6. WHERE
  7. <cfloop list="#searchString#" delimiters=" " index="word">
  8.  
  9. ShortName LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#Trim(word)#%" /> OR
  10.  
  11. </cfloop>
  12.  
  13. (1 <> 1)
  14. </cfquery>
  15.  
  16. <cfdump var="#Info#" />
If you have to search a lot of text, I suggest you use your database's FTS services or Verity.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ColdFusion Forum Timeline: dynamic form variables
Next Thread in ColdFusion Forum Timeline: coldfusion merge to word





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC