Solved

Coldfusion Page containing a search form that queries database fields

Posted on 2011-03-23
4
335 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now