Solved

Coldfusion- count list items in cfquery and cfloop

Posted on 2012-12-29
8
1,129 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

26 Experts available now in Live!

Get 1:1 Help Now