Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2012-04-13
6
234 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…

838 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