Link to home
Start Free TrialLog in
Avatar of Sandy Sailer
Sandy SailerFlag for United States of America

asked on

Having problems with CFIF Input using listcontains

I have a form which grabs the MASTER data dynamically from this query and displays it as check boxes on a form:
<cfquery name="getSCMaster" datasource="csf">
SELECT     checklistID, category, criteria, scholarshipName
FROM         dbo.tblScholarshipChecklist
WHERE            checklistID <> 1
</cfquery>

I then have a query which grabs data from another table.  The "shared" field is checklistID:
<cfquery name="getSCApp" datasource="csf">
SELECT     applicantChecklistID, applicationID, checklistID
FROM         dbo.tblApplicantChecklist
WHERE     (applicationID = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#getAppInfo.applicationID#">) AND checklistID <> 1
</cfquery>

These two tables can't be joined because I need the page to display ALL of the data from the MASTER table, and check the checklistID check box ONLY where the data from the 2nd query is valid.  

I have created a list from the 2nd query and it outputs the data correctly.  
<cfset checklistIDlist = ValueList(getSCApp.checklistID)>
<cfloop list="#checklistIDlist#" index="i">
   <cfoutput>#i# </cfoutput>
</cfloop>
For example, it would output 10 12 15.

This is where the problem starts.  The form is displaying ALL of the numbers in the list returned.  For example, if the list returns 10 12 15, it checks the following checkboxes:
2 5 10 12 15. It's not separating the list correctly and looks for any number within the list and checks that check box.  

This is the input field in my form:
<input name="checklistID" <cfif listContains("#checklistIDlist#", #getSCMaster.checklistID#)>checked="checked"</cfif> type="checkbox" id="checklistID" value="#checklistID#" />

Do I need to add a delimiter to the list somehow or do I need to loop through the list?  Help!  This is driving me crazy!!!  

Thank you!
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To address your question more directly, you are saying that the following code woud show you the correct values but the checkboxes are ALL checked.


Please try the modifications - you don't need to loop through a list to show the list.   I've cleaned up the INPUT tag to reduce unneeded code..

<cfquery name="getSCApp" datasource="csf">
  SELECT     applicantChecklistID, applicationID, checklistID
  FROM         dbo.tblApplicantChecklist
  WHERE     (applicationID = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#getAppInfo.applicationID#">) AND checklistID <> 1
</cfquery>
<cfset checklistIDlist = ValueList(getSCApp.checklistID)>

Only these should be checked: #checklistIDlist#<br>

<cfloop query="getSCMaster">
<input type="checkbox" name="checklistID" <cfif listContains(checklistIDlist, getSCMaster.checklistID)>checked="checked"</cfif> value="#getSCMaster.checklistID#" />
</cfloop>

Open in new window

Avatar of Sandy Sailer

ASKER

YOU ARE A GENIUS!!!  Absolutely PERFECT :)  
I didn't notice it before, also please change listContains() to ListFind()
<cfloop query="getSCMaster">
<input type="checkbox" name="checklistID" <cfif listFind(checklistIDlist, getSCMaster.checklistID)>checked="checked"</cfif> value="#getSCMaster.checklistID#" />
</cfloop>

Open in new window

I think you clicked the wrong button, you are attempting to accept your own answer.   Please try again and accept the answer that you're going to use :)

Don't forget listFind() is the right function, listContains does something different
@ssailer - did you mean to accept gdemaria's answer?

<cfif listContains(checklistIDlist, getSCMaster.checklistID)>

But I wanted to mention, that's the wrong function for matching whole ID's.  ListContains matches substrings.  So "2" will match "12", "214", "512" - not just the whole value "2".  For an exact match, use ListFind/FindNoCase instead.


agx, I suspect you stopped reading all the posts, which if fine - I do the same thing, but I did mention that in the last two.. :)

Author intended to accept my answer, I believe and checked his own by mistake
Yeah, I missed it.  But now they have an explanation of why use one not the other ;-)
> But now they have an explanation of why use one not the other

Yes!  perfect..