Solved

Coldfusion- count list items in cfquery and cfloop

Posted on 2012-12-29
8
1,250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 29

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
Stack Overflow Podcast - Frustrating Miracles

In this podcast, Stack Overflow interviewed Linux Academy CEO/Founder, Anthony James, and got his developer story!

"Follow your passion, be prepared to work hard and sacrifice, and, above all, don't let anyone limit your dreams."  - Donovan Bailey

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

626 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