Solved

Coldfusion Page containing a search form that queries database fields

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

947 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

23 Experts available now in Live!

Get 1:1 Help Now