Learn how to a build a cloud-first strategyRegister Now


Coldfusion checkbox values from query or value list

Posted on 2009-02-18
Medium Priority
Last Modified: 2013-11-19
I'm trying to make sure checkboxes are checked if a corresponding value exists in another query.  I have all of the add/remove stuff working, but getting the checkboxes to display as checked on an information screen is killing me.  Have tried ListGetAt, but couldn't get it to work.  I'll post my code below.

My checkboxes are created using the 'get_animals' query.

I have a second query to check to see if an existing store has that animal selected - 'cust_animals'.

What I want to do is check the box if the current checkbox's value exists in the cust_animals query.
<!--- GET ALL ANIMALS --->
<cfquery name="get_animals" datasource="#DSN#">
       	select * from lookup_animal order by animal_name asc
<cfquery name="cust_animals" datasource="#DSN#">
       	select * from customer_animals where animal_customer_id = '#this_cust.id#'
<cfoutput query="get_animals">
    <input name="animal_id" type="checkbox" value="#animal_id#" /> #animal_name#<br />
<!--- Now how do it check the box if the animal_id exists in cust_animals? --->

Open in new window

Question by:Newco
LVL 16

Expert Comment

ID: 23676894

<cfoutput query="get_animals">
	<cfif ListFindNoCase(ValueList(cust_animals.animal_id), get_animals.animal_id)>
	<!--- this ID is in the other query --->
		<input name="animal_id" type="checkbox" value="#animal_id#" checked /> #animal_name#<br />
	<!--- not found --->
		<input name="animal_id" type="checkbox" value="#animal_id#" /> #animal_name#<br />

Open in new window

LVL 52

Accepted Solution

_agx_ earned 2000 total points
ID: 23677268
Another option is to use a single query and determine that value using a CASE statement in your query. Most databases like ms sql, mysql, etc... support CASE.  If you are using Access, try IIF instead.
<!--- GET ALL ANIMALS --->
<cfquery name="get_animals" datasource="#DSN#">
   select la.animal_id, la.animal_name,
	  CASE  WHEN ca.animal_id IS NULL THEN 0 
                ELSE 1 
          END AS IsSelected	 
   from   lookup_animal la 
	    LEFT JOIN customer_animals ca ON la.animal_id = ca.animal_id
   order by animal_name asc
<cfoutput query="get_animals">
    <input name="animal_id" 
		value="#animal_id#" <cfif IsSelected>checked</cfif/> #animal_name#<br />

Open in new window


Author Closing Comment

ID: 31548579
Very good!  Thank you for your quick response - works perfect.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
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…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 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