Solved

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

Posted on 2012-04-13
6
225 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_
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:MaxwellTurner
Comment Utility
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
Comment Utility
Thanks for helping me out ... and teaching me something new!

Max
0
 
LVL 1

Author Comment

by:MaxwellTurner
Comment Utility
... typo in my code above, should be:

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

<cfoutput>#searchwords2#</cfoutput>
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now