Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Coldfusion Page containing a search form that queries database fields

Posted on 2011-03-23
4
Medium Priority
?
347 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

877 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