• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

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

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
MaxwellTurner
Asked:
MaxwellTurner
  • 4
  • 2
1 Solution
 
_agx_Commented:
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
 
MaxwellTurnerAuthor Commented:
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
 
_agx_Commented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MaxwellTurnerAuthor Commented:
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
 
MaxwellTurnerAuthor Commented:
Thanks for helping me out ... and teaching me something new!

Max
0
 
MaxwellTurnerAuthor Commented:
... typo in my code above, should be:

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

<cfoutput>#searchwords2#</cfoutput>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now