Solved

Coldfusion- count list items in cfquery and cfloop

Posted on 2012-12-29
8
1,158 Views
Last Modified: 2013-01-01
Hello experts
I need help to count the list items for each language and compare them using the following code.
Any help?
<cfoutput query="getlanguages">
  <ul class="bdesc">
   <cfif len(trim(form['Ingredients_' & languages_id]))>
    <cfset text = replace(trim(form['Ingredients_' & languages_id]),Chr(13),'^', 'all')>
    <cfset 'list_#languages_id#' = 0 >
    <cfloop list="#text#" index="i" delimiters="^">
     <cfset 'list_#languages_id#' =  ['list_' &  languages_id] + 1>
     <li>#trim(i)#</li>
    </cfloop>
   </cfif>
  </ul>
  <cfdump var="">
</cfoutput>
0
Comment
Question by:Panos
  • 4
  • 3
8 Comments
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 38730960
Looping over the form fields (name matching = 'Ingrednents' & language_id), you can build a
query.

Here is a outline of the code,

1. Use QueryNew() to define the query structure.
     <cfset lqry=
2. Loop over the form field names and add to query. Use QueryAddRow() and QuerySetCell()
3. One you have your new query structure populated, you can count.

Here is code, (not tested )

<!--- Define Query --->
<cfset myQuery = QueryNew("fieldname, fieldvalue", "VarChar, varchar")>
<!--- Start Populating --->
<cfloop list="#form.fieldnames#" index="fld">
    <cfset tt= QueryAddRow(MyQuery, 1)/>
    <cfset tt= QuerySetCell(myQuery, "fieldname", #fld#)/>
    <cfset tt= QuerySetCell(myQuery, "fieldvalue", form[#fld#])/>
</cfloop>


<!--- get the field count --->
<cfloop query="getlanguages">
<cfquery name="group_by" dbtype="query">
    SELECT fieldname AS x, count(fieldname) as fieldCnt  
    FROM myquery  
    WHERE fieldname like 'Ingredients_' & #getLanguages.language_id
    GROUP BY fieldname
</cfquery>
<cfoutput><br/>#group_by.fieldcnt#: #group_by.fieldcnt#</cfoutput>
</cfloop>
0
 
LVL 2

Author Comment

by:Panos
ID: 38731001
Hmmm.
Hi  pravinasar.
I think you did not understand what i'm trying to do or i did not understand your code.
I have a formfield "Ingredients_' & languages_id"
The number of the Ingredients_ fields depends on how many languages i have activated.
A user can write inside that field the ingredients of a recipe spliting them using the enter key. I 'm using hidden - show tabs for each language.
To tranform the entered values to a list i'm using:
<cfset text = replace(trim(form['Ingredients_' & languages_id]),Chr(13),'^', 'all')>
I will add to each step one image upload field but not for each language separate.
the problem now is that the user can make any mistake with the number of Ingredients.
That what i was looking for was to find a way to count and compare the lists of every language.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38731222
I'm not completely sure what you're after - and unfortunately I can't stick around today. But maybe this example might help (?)

<!--- sample data --->
<cfset getLanguages = queryNew("")>
<cfset queryAddColumn(getLanguages, "languages_id", listToArray("1,2,3,4"))>

<cfset FORM.Ingredients_1 = "">
<cfset FORM.Ingredients_2 = "foo"& chr(13) & "bar">
<cfset FORM.Ingredients_3 = "">
<cfset FORM.Ingredients_4 = "foo"& chr(13) & "bar"& chr(13) & "qax">

<cfset languageTotals = {}>
<cfoutput query="getLanguages">
  <ul class="bdesc">
   	<cfif len(trim(form['Ingredients_' & languages_id]))>
	    <cfset text = replace(trim(form['Ingredients_' & languages_id]),Chr(13),'^', 'all')>
	    <cfset total = 0>
		<cfloop list="#text#" index="i" delimiters="^">
	    	<cfset total = total+ 1>
	    	<li>#trim(i)#</li>
		</cfloop>
		<cfset languageTotals[languages_id] = total>
		<li><strong>items:</strong> #languageTotals[languages_id]#</li>
	</cfif>
  </ul>
</cfoutput> 

<!--- compare all --->
<cfloop collection="#languageTotals#" item="id">
	<cfoutput>
 	total for languages_id[#id#] = #languageTotals[id]#<br>
	</cfoutput>
</cfloop>

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Author Comment

by:Panos
ID: 38731600
Hi agx.
It is almost what i need.
I need a cfif tag that is comparing all languagetotals. If they are all the same.....
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 38732955
To just verify the counts are the same, an array would be better:

<!--- sample data --->
<cfset getLanguages = queryNew("")>
<cfset queryAddColumn(getLanguages, "languages_id", listToArray("1,2,3,4"))>

<cfset FORM.Ingredients_1 = "">
<cfset FORM.Ingredients_2 = "foo"& chr(13) & "bar">
<cfset FORM.Ingredients_3 = "">
<cfset FORM.Ingredients_4 = "foo"& chr(13) & "bar"& chr(13) & "qax">

<cfset allTotals = []>
<cfoutput query="getLanguages">
  <ul class="bdesc">
   	<cfif len(trim(form['Ingredients_' & languages_id]))>
	    <cfset text = replace(trim(form['Ingredients_' & languages_id]),Chr(13),'^', 'all')>
	    <cfset total = 0>
		<cfloop list="#text#" index="i" delimiters="^">
	    	<cfset total = total + 1>
	    	<li>#trim(i)#</li>
		</cfloop>
		<cfset arrayAppend(allTotals, total)>
	</cfif>
  </ul>
</cfoutput> 

<cfif arrayMin(allTotals) neq arrayMax(allTotals)>
	amounts are not the same. do something here ...
</cfif>

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 38734320
Thank you agx.
It is working.

Happy new year
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 38734321
Thank you
regards
panos
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38735139
Welcome. Happy new year :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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