Solved

Coldfusion Page containing a search form that queries database fields

Posted on 2011-03-23
4
337 Views
Last Modified: 2012-05-11
I currently have a coldfusion page that contains a search field that allows a user to enter a search term that could be any string.  I have my code currently looking for spaces in the string and replacing them with % so that my query has wildcards in the search string.  My code for the form, form processing and query are below.

Currently when a user enters a search term such as 7ft metal it will return anything that matches those keywords in that order.  I wanted to know if there is a good way to make it so that the search terms can be in any order in the database field so that search will return items that are labeled metal 7ft as part of their description.

Any help would be appreciated.

 
/*Form on the page/*
    <form action="inventorysearch.cfm" method="post" name="inventorysearch">
<input name="invsearch" type="text"  size="25" />
<SELECT  NAME="Category" onChange="SelectSubCat();" >
<Option value="All">All Categories</option>
</SELECT>&nbsp;
<SELECT id="SubCat" NAME="SubCat">
<Option value="All">All Sub Categories</option>
</SELECT>
<input name="Search" type="submit" value="Search" /></form>

/*Form Processing Code */
<cfset searchterm=Replace(Form.invsearch, " ","%","ALL")>
<cfset categoryList=Form.Category>

/*Query*/
        <cfquery datasource="UnifyPos" name="products">
    SELECT "PUB"."item"."item##" AS itemnum, "PUB"."item"."dept##" AS deptnum, "PUB"."item"."subdept##" AS subdeptnum, "PUB"."item".price, "PUB"."item".descrpt AS item_descript, "PUB"."dept".dscrpt AS dept_descript, "PUB"."totals".tot as inventory, "PUB"."subdept".descrpt AS subdept_descript
    FROM "PUB"."item" JOIN "PUB"."dept" ON "PUB"."item"."dept##"="PUB"."dept"."dept##" JOIN "PUB"."totals" ON "PUB"."item"."item##"="PUB"."totals"."itemnum" JOIN "PUB"."subdept" ON "PUB"."item"."subdept##"= "PUB"."subdept"."subdept##"
    WHERE "PUB"."totals"."period" = 'd' AND (upper(item_descript) LIKE upper('%#searchterm#%') AND  "PUB"."item"."subdept##" IN (<cfqueryparam value="#subcategoryList#" list="true" cfsqltype="CF_SQL_INTEGER">) 
    ORDER BY  subdept_descript, item_descript

    </cfquery>

Open in new window

0
Comment
Question by:sigkappu
  • 3
4 Comments
 
LVL 23

Accepted Solution

by:
OP_Zaharin earned 500 total points
ID: 35203414
hi sigkappu,
- what i would suggest is to separate the search criteria into an array if the loop find spaces in between the criteria.
- the codes i prepared below will separate the 7ft and metal into two different search criteria therefore it will search for item_descript LIKE '%7ft%' OR item_descript LIKE '%metal%'
- the 1 <> 1 is simply to handle the OR function for the last loop, if not the syntax will give error
- not sure if this is what you looking for though:

<cfset searchterm = Form.invsearch>
 
<cfquery datasource="UnifyPos" name="products"
    SELECT "PUB"."item"."item##" AS itemnum, "PUB"."item"."dept##" AS deptnum, "PUB"."item"."subdept##" AS subdeptnum, "PUB"."item".price, "PUB"."item".descrpt AS item_descript, "PUB"."dept".dscrpt AS dept_descript, "PUB"."totals".tot as inventory, "PUB"."subdept".descrpt AS subdept_descript
    FROM "PUB"."item" JOIN "PUB"."dept" ON "PUB"."item"."dept##"="PUB"."dept"."dept##" JOIN "PUB"."totals" ON "PUB"."item"."item##"="PUB"."totals"."itemnum" JOIN "PUB"."subdept" ON "PUB"."item"."subdept##"= "PUB"."subdept"."subdept##"
    WHERE 
    "PUB"."totals"."period" = 'd' AND     
    "PUB"."item"."subdept##" IN (<cfqueryparam value="#subcategoryList#" list="true" cfsqltype="CF_SQL_INTEGER">) 

    <cfloop list="#searchterm#" delimiters=" " index="searchword">	 
            upper(item_descript) LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#upper(Trim(searchword))#%"> OR 
    </cfloop>

    1 <> 1 
    ORDER BY  subdept_descript, item_descript
</cfquery>   

Open in new window

0
 
LVL 23

Expert Comment

by:OP_Zaharin
ID: 35203463
- sorry that i left out the closing:
<cfquery datasource="UnifyPos" name="products">
0
 

Author Closing Comment

by:sigkappu
ID: 35206264
This was exactly what I was trying to accomplish.  Thank you very much for your help.
0
 
LVL 23

Expert Comment

by:OP_Zaharin
ID: 35206485
hi sigkappu, glad its help :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Read about achieving the basic levels of HRIS security in the workplace.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

778 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