?
Solved

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

Posted on 2012-04-13
6
Medium Priority
?
244 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
[X]
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
  • 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 2000 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

765 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