Solved

Trouble with CFLOOP and Creating a list

Posted on 2007-04-01
3
214 Views
Last Modified: 2012-08-13
I cannot seem to be able to create a list of names from a list of id's. Keeps doubling the list up when all I want is one list.
I need "John,Sara,Jack,Jill"
I have "112,115,97,420"
I want to query the DB and get the names from the id and create a comma list like above, however they keep doubling based on my code below. Any idea how to fix this?

Here's the code
<cfset mylist = "112,115,97,420">
<cfset newemplist = "">
<cfloop index="newlist" list="#mylist#" delimiters=",">
      <cfquery name="getemp" datasource="DB">
            select * from employees where emp_id = #newlist#
      </cfquery>
      <cfoutput query="getemp">
            <cfif #newemp# eq "">
                  <cfset newemp = "#document#">
            <cfelse>
                  <cfset newemp = "#newdoc#,#document#">
            </cfif>
      </cfoutput>

</cfloop>

It creates John John,Sara, John John, Sara, John, Sara, Jack, John John, Sara, John John, Sara,Jill..... Or something like that.  Any ideas would be great! Thanks
0
Comment
Question by:dudeatwork
  • 2
3 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 18832270
try something like this (you need to customize a bit accordingly):

<cfset mylist = "112,115,97,420">
<cfset newemplist = "">

<cfquery name="getemp" datasource="DB">
            select * from employees where emp_id in ( #newlist# )
      </cfquery>

<cfloop query="getemp">
          <cfif newemplist = "">
                <cfset newemplist = #getemp.yourFieldName#>
          <cfelse>
                <cfset newemplist = newemplist & ", " & #getemp.yourFieldName#>          
          </cfif>
</cfloop>

<cfoutput>Name list = #newemplist#</cfoutput>


hope this helps
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 18834014
<cfset newemp = "#newdoc#,#document#">
I don't see where NewDoc and Document are coming from?


What you want to do is append the field name to your list variable each time..

<cfset newEmp = "">
<cfoutput query="getemp">
   <cfset newEmp = listAppend(newEmp, document)>
</cfoutput>
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 18836095

 Here's another way to do it that will keep you from having to run a query again and again within the loop.

 Just fetch all employess who's ID is on the list and use that query to create your ID and your name list.   Its important to recreate your ID list!!!  Because the order of the names fetched may not match the order of the IDs in your original list (so the names would be out of order).  But if you just create the ID list again, you'll be all set.

<cfset mylist = "112,115,97,420">

<cfquery name="getemp" datasource="DB">
  select empID, firstName
  from employees where emp_id in (#myList#)
  order by firstName -- you can change the order of the list here
</cfquery>
<cfset empNames = valueList(getEmp.firstName)>
<cfset empIDs = valueList(getEmp.empID)>  <!--- create ID list as well !! ---->

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

11 Experts available now in Live!

Get 1:1 Help Now