Solved

Coldfusion Page containing a search form that queries database fields

Posted on 2011-03-23
4
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 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