Solved

Highlight multiple "search" parameters within the resulting "search results"  string

Posted on 2012-04-13
6
233 Views
Last Modified: 2012-04-13
I have a "search" form with a single text box.  The user can enter one or more words (SEARCHPARAM) and submit to form which will display the search results (SEARCHWORDS).  I want to highlight the "searchparam" words that they typed in within the "searchword" results.

It is easy to do it if the user types in just one search word:

#replaceNoCase(searchwords,searchparam,"<span style='background:yellow;'>#searchparam#</span>","all")#

How can I do this if the user enters more than one search word?  I can hard-code something like this if there are, for example, 3 words:

HARDCODED EXAMPLE:
#replaceNoCase(replaceNoCase(replaceNoCase(searchwords,listgetat(searchparam,1,' '),"<span style='background:yellow;'>#listgetat(searchparam,1,' ')#</span>","all"),listgetat(searchparam,2,' '),"<span style='background:yellow;'>#listgetat(searchparam,2,' ')#</span>","all"),listgetat(searchparam,3,' '),"<span style='background:yellow;'>#listgetat(searchparam,3,' ')#</span>","all")#

... but I need it do create the code on the fly based on the number of searchwords.  I tried creating to code above using a series of loops and cfset, but I can only get it to display the code, not the highlighted results:

<cfset multiwords = "">

<cfloop from="1" to="#listlen(searchparam,' ')#" index="y">
     <cfset multiwords = multiwords & "replaceNoCase(">
</cfloop>
<cfset multiwords = multiwords & "searchwords, ">

<cfset row = 0>
<cfloop from="1" to="#listlen(searchparam,' ')#" index="x">
      <cfset row = row + 1>
      <cfset multiwords = multiwords & "#listgetat(searchparam,x,' ')#, '<span style='background:yellow;'>#listgetat(searchparam,x,' ')#</span>','all') <cfif row5 lt listlen(searchparam,' ')>, </cfif>">
</cfloop>

<cfloop from="1" to="#listlen(searchparam,' ')#" index="x">
     #multiwords#
</cfloop>

This will display the code just like my hard-coded example above, but not the highlighted search results.

Any Ideas?  I feel sooo close ...

Max
0
Comment
Question by:MaxwellTurner
  • 4
  • 2
6 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 37844853
Just loop through the list of params.

<cfset searchparam = "results words single the">
<cfloop list="#searchParam#" index="param" delimiters=" ">
      <cfset searchwords = replaceNoCase(searchwords, "#param#","<span style='background:yellow;'>#param#</span>","all")>      
</cfloop>

While the above works in most cases the regex doesn't account for special chars.  A regex expert could probably help improve it by finding whole words, and preserve case, etc...
0
 
LVL 1

Author Comment

by:MaxwellTurner
ID: 37844985
Hi Thanks,

That actually was one of the first things I tried, but when I would cfoutput #searchwords# as shown above in your eg, only the last item of the searchparam list is highlighted, using your example, the word "the".  If I output #searchwords# within the loop, the entire thing is repeated x number of times depending on the 'searchparam' list length.

I am ultimately trying to optimize my search so that if there are eg. 3 searchwords submitted and found within the column being searched (I am only searching one varchar(max) column), it should be at the top with all 3 words highlighted ... only 2 matches found would follow below and so on ...

Not ideal, but what I have ended up doing for now is ...

<cfif listlen(searchparam,' ') eq 1>
     Replacenocase(...
<cfelseif listlen(searchparam,' ') eq 2>
     Replacenocase(Replacenocase(...
<cfelseif listlen(searchparam,' ') eq 3>
     Replacenocase(Replacenocase(Replacenocase(...

right up to 10 ... very unlikely there will ever be more than 3-4, so this should be safe.

Max
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 37845147
Are you sure you used the code above? Here's the string I tested with and it highlights all words:
<cfsavecontent variable="searchWords">
I have a "search" form with a single text box.  The user can enter one or more words (SEARCHPARAM) and submit to form which will display the search results (SEARCHWORDS).  I want to highlight the "searchparam" words that they typed in within the "searchword" results...
... but I need it do create the code on the fly based on the number of searchwords.  I tried creating to code above using a series of loops and cfset, but I can only get it to display the code, not the highlighted results:
</cfsavecontent>

<cfset searchparam = "results words single the">
<cfloop list="#searchParam#" index="param" delimiters=" ">
	<cfset searchwords = replaceNoCase(searchwords, "#param#","<span style='background:yellow;'>#param#</span>","all")>	
</cfloop>

<cfoutput>#searchWords#</cfoutput>

Open in new window


If I output #searchwords# within the loop, the entire thing is repeated x number of times depending on the 'searchparam' list length.

That's because the earlier code was appending the string to itself each time, instead of replacing it. Likewise if you're only seeing one word replaced it's because the code isn't using the updated string for subsequent replaces like the example above.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:MaxwellTurner
ID: 37845193
Okay got it working now using:

<cfsavecontent variable="SearchWords2">#searchwords#</cfsavecontent>
and then
<cfset searchwords = replaceNoCase(searchwords2, ....

<cfoutput>#searchwords2#</cfoutput>

Never used cfsavecontent before ... handy, I'll hang onto that!  Thanks!

Max
0
 
LVL 1

Author Closing Comment

by:MaxwellTurner
ID: 37845194
Thanks for helping me out ... and teaching me something new!

Max
0
 
LVL 1

Author Comment

by:MaxwellTurner
ID: 37845198
... typo in my code above, should be:

<cfsavecontent variable="SearchWords2">#searchwords#</cfsavecontent>
and then
<cfset searchwords2 = replaceNoCase(searchwords2, ....

<cfoutput>#searchwords2#</cfoutput>
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question