Skip to main content
March 11, 2009
Question

Search results

  • March 11, 2009
  • 1 reply
  • 312 views
Yellow.

How do you return the string that was found in bold when you query several different columns of a table?

Example

<cfquery name="find_yellow" datasource="colors">
SELECT id
FROM colors
WHERE color_name = "yellow"
OR related_color = "yellow"
</cfquery>

<cfoutput query="find_yellow">
Display the value that was found in bold
</cfoutput>

    This topic has been closed for replies.

    1 reply

    Participating Frequently
    March 12, 2009
    You'd have to loop over each word and see if it matches.

    <cfparam name="URL.Search" default="">

    <cfquery name="find_yellow" datasource="colors">
    SELECT col1, col2
    FROM colors
    WHERE col1 like '%#URL.Search#%'
    OR col2 like '%#URL.Search#%'
    </cfquery>

    (*note: you'd wanna use cfqueryparam in the above to prevent against SQL injection)

    <cfoutput query="find_yellow">
    #highlight(URL.Search,col1)#
    #highlight(URL.Search,col2)#
    </cfoutput>

    <cffunction name="highlight" output="true">
    <cfargument name="match">
    <cfargument name="string">

    <cfoutput>
    <cfloop list="#string#" delimiter=" " index="aWord">
    <cfif aWord eq Match>
    <b>#aWord#</b>
    <cfelse>
    #aWord#
    </cfif>
    </cfloop>
    </cfoutput>

    </cffunction>