ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Learn five alternative approaches for automating the delivery of Excel-based reports. Read all about it in the free whitepaper: “Automating Excel Reports: Five Approaches for Java DevelopersDownload Now!
  #1  
Old May 14th, 2004, 12:10 AM
ResonantEvil ResonantEvil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 14 ResonantEvil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
Form Queries

Ok so I want to write a query based on form input. The select boxes, and text boxes are easy. The think I have not a schmick about is how to base a query on checkboxes. Essentially I want to list all records that contain a certain (yes/no) field, IF the user checked the box. Anyone have any ideas?

Reply With Quote
  #2  
Old May 14th, 2004, 09:29 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,514 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 21 h 17 m 26 sec
Reputation Power: 45
You can just check to see if the checkbox form field is defined using isDefined( 'form.myCheckboxName' ). If it is, the values in the form variable will be a list of the checkbox values the user checked. You can loop over the list and output whatever SQL you need to within the query.

Reply With Quote
  #3  
Old May 14th, 2004, 09:39 AM
bfolger71 bfolger71 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Boston, MA
Posts: 47 bfolger71 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 34 sec
Reputation Power: 5
Hi -

Not exactly sure what your end goal is... You mention that you want to list all records that "contain" a certain field. Do you mean you want to list all records that have a certain value for that certain field?

Checkboxes are essentially passed as lists. Multiple checkboxes can have the same NAME attribute. Each checkbox is either CHECKED (and the value is concatenated to the name variable) or it is not checked (no value is passed for this instance).

Here's an example:

<input type=checkbox name=testCheckbox value=1>
<input type=checkbox name=testCheckbox value=3>
<input type=checkbox name=testCheckbox value=5>
<input type=checkbox name=testCheckbox value=8>

When passed, testCheckbox will have the value of whatever is checked, as a comma-delimited list. Assuming all are checked, testCheckbox would have the value: 1,3,5,8 in the above example.

To build a query based on this, you would just test to see if the passed variable contained a specific value. If it did, you would add a WHERE clause for the associated value to the SQL statement:

<cfquery datasource="#db_source#" name="getList">
SELECT *
FROM table
WHERE 0 = 0
<cfif FORM.testCheckbox CONTAINS "1">
AND YESNOfieldname_1 = "yes"
</cfif>
<cfif FORM.testCheckbox CONTAINS "3">
AND YESNOfieldname_3 = "yes"
</cfif>
.... so on for each of the possible fields
</cfquery>

Is that what you're looking to do?

-------------------------------

Just realized that the CONTAINS is not a good option. The conditional checking for the existence of "1" would return TRUE for 1, 10, 17, 31, 100.... you get the point. kiteless' approach of looping through to generate the query is much better...

Last edited by bfolger71 : May 14th, 2004 at 09:45 AM.

Reply With Quote
  #4  
Old May 14th, 2004, 06:58 PM
ResonantEvil ResonantEvil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 14 ResonantEvil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
Ok this code:
Code:
<cfquery name="Result" datasource="listinginfo">
select * from listings
where baths = #form.baths#
<cfif IsDefined("Form.ensuite")>
AND where ensuite = 'Yes'
</cfif>
order by listings.listing_number
</cfquery>

Gives the following error:

Quote:
Error Executing Database Query.
[Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'baths = 2 AND where ensuite = 'Yes''.


Anyone know why?

Reply With Quote
  #5  
Old May 15th, 2004, 11:14 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,514 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 21 h 17 m 26 sec
Reputation Power: 45
What data type is the column "ensuite"? You must make the query match the data type of the column. So if ensuite is boolean, try:

ensuite = yes

or:

ensuite = true

or maybe:

ensuite = 1

Reply With Quote
  #6  
Old May 15th, 2004, 07:43 PM
ResonantEvil ResonantEvil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 14 ResonantEvil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
Thanks for the help guys! I got it figured out. Turns out I had extra 'WHERE' statments in there. Now I have a new problem. I want to check against a select box. It's a little tricky though, because instead of a simple equality (WHERE form.x = y) I need to check it ONLY if a certain option is NOT selected (my N/A option indicated that the user doesn't care about that field). This is the code I have to check it:

Code:
<cfif (#form.type# NEQ 'N/A')>
and (#form.type# = type)
</cfif>


The problem is that it returns the following error:
Quote:
[Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.


#form.type# is the form variable, which is a select box. 'type' is from my db, and is a string datatype.

I don't understand why it's not working.

Reply With Quote
  #7  
Old May 16th, 2004, 10:42 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,514 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 21 h 17 m 26 sec
Reputation Power: 45
Can you post the SQL that CFMX is trying to run? It should be in the debugging info or the error information.

Reply With Quote
  #8  
Old May 18th, 2004, 02:07 PM
ResonantEvil ResonantEvil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 14 ResonantEvil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
How do I output the debug info? I Unselected database activity in Administrator, and put ' debug="yes" ' in my cfquery, but I don't see and debugging information.

Reply With Quote
  #9  
Old May 18th, 2004, 02:50 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,514 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 21 h 17 m 26 sec
Reputation Power: 45
You must enable CF debugging under the Debugging section of the CF administrator.

Reply With Quote
  #10  
Old May 18th, 2004, 03:05 PM
ResonantEvil ResonantEvil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 14 ResonantEvil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
Ok this is all I get out of the debugging screen:

Quote:
Exceptions
13:03:16.016 - Database Exception - in C:\CFusionMX\wwwroot\HumbleAbode-Victoria\RentSearchResult.cfm : line 57

Error Executing Database Query.

Reply With Quote
  #11  
Old May 18th, 2004, 03:27 PM
ResonantEvil ResonantEvil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 14 ResonantEvil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
Ok it's weird, when I take that code out everything works, when I put it back in I get nothing, and the error occurs on the </cfif> tag in the last conditional statement. Here's what I get:

Quote:
Error Executing Database Query.
[Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

The error occurred in C:\CFusionMX\wwwroot\HumbleAbode-Victoria\RentSearchResult.cfm: line 58

56 : <cfif (#form.type# is 'Apartment')>
57 : and #type# = "Apartment"
58 : </cfif>
59 :
60 :

SQL select * from listings where ((0 <= rent) AND (rent <= 5000)) and (baths >= 0) and Apartment = "Apartment" order by listings.listing_number
DATASOURCE listinginfo
VENDORERRORCODE -3010
SQLSTATE 07002

Reply With Quote
  #12  
Old May 18th, 2004, 03:38 PM
bfolger71 bfolger71 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Boston, MA
Posts: 47 bfolger71 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 34 sec
Reputation Power: 5
Quote:
Originally Posted by ResonantEvil
SQL select * from listings where ((0 <= rent) AND (rent <= 5000)) and (baths >= 0) and Apartment = "Apartment" order by listings.listing_number


Is there a field in your table called "Apartment"? Or is the field supposed to be "Type"? In the query above, you're checking to see if the field Apartment = 'Apartment'....

Try this?

56 : <cfif (form.type is 'Apartment')>
57 : and listings.type = "Apartment"
58 : </cfif>

Reply With Quote
  #13  
Old May 18th, 2004, 04:35 PM
ResonantEvil ResonantEvil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 14 ResonantEvil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
I tried your code and got the same problem. And yes the form field is called type. "Apartment" is one possible value.

Reply With Quote
  #14  
Old May 22nd, 2004, 01:18 PM
ResonantEvil ResonantEvil is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 14 ResonantEvil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
No one knows eh?

Reply With Quote
  #15  
Old May 22nd, 2004, 03:35 PM
bfolger71 bfolger71 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Boston, MA
Posts: 47 bfolger71 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 34 sec
Reputation Power: 5
Resonant:

From what you've described, the following doesn't seem like a valid SQL statement against your DB:

SQL select * from listings where ((0 <= rent) AND (rent <= 5000)) and (baths >= 0) and Apartment = "Apartment" order by listings.listing_number

Do you have a *database* field called "Apartment"? Or is the database field actually "type", and one possible value for that field is "Apartment"? If it's the latter, your query is wrong. You need to remove the #'s around type. That's what I was trying to point out previously...

The only other thing I can recommend at this point is to rearrange your SQL statement slightly:

SELECT * from listings where ((rent >= 0) AND (rent <= 5000)) and (baths >= 0) and type = "Apartment" order by listings.listing_number

You also might try using BETWEEN in your statement. r937 should have some input here...

Hope this helps.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > Form Queries


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump