Skip to main content
Inspiring
Aug 18, 2006 4:49 PM UTC
Answered

keyword searching - displaying only the matched field

Hi,
I need some help writing a <Cfoutput> which displays only the fieldname which matched a search.

FYI, For example I have a form with a keyword field allows a user to do a "search by last name" that searches my very basic table which has multiple type of last names (ex. director_lastname, actor_lastname, etc.)

<cfquery name="Results" datasource="my_dsn">
SELECT * FROM my table WHERE
director_lastname LIKE '%#keyword#%' OR actor_lastname LIKE '%#keyword#%'
</cfquery>

Currently I can only figure out how to cfoutput the entire row that matched the querey

As always, Any help is greatly appreciated!
Thanks!
This topic is closed to new replies. Start a new post to keep the conversation going.
Dan_BracukCorrect answer
Inspiring
Aug 18, 2006 7:32 PM UTC
For syntax, change this
<cfif #director_lastname# CONTAINS '%#keyword#%'>
to this
<cfif director_lastname CONTAINS keyword>

to be more effiecient
<cfoutput query='yourquery">
<cfloop list=yourquerycolumnlist index="thisitem">
<cfif thisitem contains keyword>
#thisitem# #evaluate(thisitem)#
closing tags

or something like that.

3 replies

Inspiring
Aug 18, 2006 4:58 PM UTC
Loop through the columnlist and use the contains function. You realize of course that you could get more than one record, and they might match on different fields.
Translate
Inspiring
Aug 18, 2006 6:50 PM UTC
Thanks,

I tried looking up contains and cfloop and am having some trouble.
Can you help me with the syntax.

I tried something like this but it isn't working. I must confess I never needed thus never used cfloop before..
I guess there is a first time for everything..

<cfquery name="Results" datasource="my_dsn">
SELECT * FROM my table WHERE
director_lastname LIKE '%#keyword#%' OR actor_lastname LIKE '%#keyword#%'
</cfquery>

<cfloop query="results">
<cfif #director_lastname# CONTAINS '%#keyword#%'>
<cfoutput query="results">#director_lastname# </cfoutput>
<cfelse>
</cfif>

<cfif #actor_lastname CONTAINS '%#keyword#%'>
<cfoutput query="results">#actor_lastname# </cfoutput>
<cfelse>
</cfif>
</cfloop>

Any sugestions? I know that it results may end up in differnet column, but haven't thought through how difficult this cfoutput would be.
Translate
Dan_BracukCorrect answer
Inspiring
Aug 18, 2006 7:32 PM UTC
For syntax, change this
<cfif #director_lastname# CONTAINS '%#keyword#%'>
to this
<cfif director_lastname CONTAINS keyword>

to be more effiecient
<cfoutput query='yourquery">
<cfloop list=yourquerycolumnlist index="thisitem">
<cfif thisitem contains keyword>
#thisitem# #evaluate(thisitem)#
closing tags

or something like that.
Translate