Need help with advanced search please!!

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Need help with advanced search please!!

    I am trying to create a search that searches two fields and highlights the
    search term. How can I do this? Here is my code as of now:

    <cfquery name="addfaq" datasource="stuff">
    SELECT *
    FROM FAQ
    WHERE (0=0
    <CFLOOP list="%#FORM.search#%" index="thisword" delimiters=" ">
    AND (Answer LIKE '%#thisword#%' OR Question LIKE '%#thisword#%')
    </CFLOOP>)
    ORDER BY ID ASC


    And I have this in the body:

    <cfoutput query="addfaq" startRow="#StartRow_addfaq#"
    maxRows="#MaxRows_addfaq#">
    <tr>

    <td><div align="center">#addfaq.Date#</div></td>
    <td><div align="center"><a
    href="faq_detail.cfm?recordID=#addfaq.ID#">#addfaq .ItemNumber#</a></div></td>

    <td>#Replace(addfaq.Question, "%#FORM.search#%", "<span
    bgcolor='yellow'>%#FORM.search#%</span>", "ALL")#</td>
    <td><div align="center">#addfaq.Answer#</div></td>
    <td><div align="center">#addfaq.Verified#</div></td>

    </tr>
    </cfoutput>


    Any help on this would be great!!!

    EnergyFed Guest

  2. Similar Questions and Discussions

    1. advanced search with php
      Does anybody know of a good tutorial on how to do a search for any/all words with php? Thanks
    2. Advanced search (Verity)
      Somebody, please help me! Tell me how I can search and select from results entries of sought word only within title, body, URL, alternate text and...
    3. advanced search
      hi, I building a advanced search funcationality with 3 listbox and 5 checkbox so want I want that you can search with all possible ways' cross...
    4. ANN: InterAKT Site Search - search in multiple tables
      Hello, We have just released a new product, MX Site Search, meant to help web developers and designers create a search form in their dynamic...
    5. #25786 [NEW]: PHP website uses cookies to remember last search phrase in search box
      From: tipsen at imada dot sdu dot dk Operating system: - PHP version: Irrelevant PHP Bug Type: Unknown/Other Function Bug...
  3. #2

    Default Re: Need help with advanced search please!!

    Your code looks like it should work except for a few things:
    1) bgcolor is not a valid attribute for <span> so this will not work on many
    (any?) browsers.
    2) FORM.search is a list of words, so they must be highlighted separately.
    3) "%#FORM.search#%" should probably be "#FORM.search#"

    Also,
    Do you want to highlight the words in the answer too?
    Do you want the highlight to be case sensitive? Is the search?

    Replace your body with the attached code. I have not tested it so beware of
    typos.

    Cheers,
    -- MikeR



    <!--- Local function for easier coding. --->
    <CFFUNCTION name="sHighLightKeyWords" returnType="string" output="no">
    <CFARGUMENT name="sRawText" type="string" required="Yes">
    <CFARGUMENT name="sListOfKeyWords" type="string" required="Yes">
    <CFARGUMENT name="sListDelimiter" type="string" default=" ">

    <CFSET var sProcessedText = sRawText>

    <CFLOOP list="#sProcessedText#" index="sKeyWord"
    delimiters=#sListDelimiter#>
    <CFSET sProcessedText = ReplaceNoCase
    (
    sProcessedText,
    sKeyWord,
    '<span
    class="HighLighted">#sKeyWord#</span>',
    "ALL"
    )
    >
    </CFLOOP>
    <CFRETURN sProcessedText>
    </CFFUNCTION>


    <!--- Use CSS for cleaner, easier html. --->
    <CFOUTPUT>
    <style type="text/css">
    .HighLighted
    {
    background-color: yellow;
    }

    td.CenterCell
    {
    text-align: center;
    }
    </style>
    </CFOUTPUT>


    <!--- Note that it may be more efficient to save this entire loop's output
    using
    cfsavecontent, and then do the string replace only once.
    --->
    <CFOUTPUT query="addfaq" startRow="#StartRow_addfaq#"
    maxRows="#MaxRows_addfaq#">
    <tr>
    <td class="CenterCell">#addfaq.Date#</td>
    <td class="CenterCell"><a
    href="faq_detail.cfm?recordID=#addfaq.ID#">#addfaq .ItemNumber#</a></td>
    <td class="CenterCell">#sHighLightKeyWords (addfaq.Question,
    FORM.search)#</td>
    <td class="CenterCell">#sHighLightKeyWords (addfaq.Answer,
    FORM.search)#</td>
    <td class="CenterCell">#addfaq.Verified#</td>
    </tr>
    </CFOUTPUT>

    MikerRoo Guest

  4. #3

    Default Re: Need help with advanced search please!!

    Thanks, that works great!
    EnergyFed Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139