Skip to main content
November 8, 2007
Question

Highlight keyword in search results.

  • November 8, 2007
  • 4 replies
  • 1002 views
Greetings

I did this before and can't seem to replicate the code- but,

1) pass the form variable "form.keyword" to the results page
2) check that it exists (<cfif isDefined ....>)
3) the page header has: <style type="text/css">.highlight{background-color:yellow;}
</style>
4) highlight the keyword in the results page- that's it- nothing fancy- e.g. it's ok if searching for "cement" returns the word "replacement".

Thanks in advance!
    This topic has been closed for replies.

    4 replies

    Inspiring
    November 9, 2007


    Take Care
    November 9, 2007
    Thanks Glen-

    This works- (<CFSET> within the <cfoutput>)

    <cfoutput query="findcode">
    <CFSET TheOutput = "#cccode_num# - #cccode_desc#">
    #ReplaceNoCase("#TheOutput#","#form.keyword#","<span class=highlight>#form.keyword#</span>","ALL")#<br></cfoutput>


    Have a great weekend...
    November 8, 2007
    Thanks for the quick response...

    Could you reveal exactly how this would look on the display page?

    the page is now:

    <html>.......

    <style type="text/css">.highlight{background-color:yellow;}
    </style>
    </head>
    <body>
    <cfquery name="findcode" datasource="vendors">
    SELECT *
    FROM all_cccode_lookup
    WHERE cccode_desc LIKE '%#form.keyword#%'
    </cfquery>


    <font face="Verdana, Arial, Helvetica, sans-serif" size="+1" color="navy">
    <p>Your search returned <cfoutput>#findcode.RecordCount#</cfoutput> code(s).</p>

    <p>Results of your Search:</font></p>
    <font face="sans-serif" size="1">
    <cfoutput query="findcode">#cccode_num# - #cccode_desc#<br></cfoutput>


    Thanks so much for your time....
    Inspiring
    November 9, 2007
    I usually combine my output into 1 variable first so it makes it easier to do the replacing:

    The only thing that really changes is the "output" part of the page.


    <p>Results of your Search:</font></p>
    <font face="sans-serif" size="1">

    <cfoutput query="findcode">
    <CFSET TheOutput = "#cccode_num# - #cccode_desc#">

    #ReplaceNoCase("#TheOutput#","#form.keyword#","<span class=highlight>#form.keyword#</span>","ALL")#<br>
    </cfoutput>


    Inspiring
    November 8, 2007
    I usually just use ReplaceNoCase; have it replace all the instances of the selected word with the word + a <span> tag around it with the required style class.