List variables in a loop

I want to count the variables and show them somewhere in my code. I have a loop:

<cfloop query="get_serial"> <cfif PROCESS_ID eq attributes.action_id> #SERIAL_NO# </cfif> </cfloop> 

and his request:

 <cfquery name="get_serial" datasource="#dsn3#"> SELECT * FROM SERVICE_GUARANTY_NEW WHERE STOCK_ID = #attributes.action_row_id# ORDER BY SERIAL_NO </cfquery> 

Everything works fine, but I want to calculate how many variables are displayed exactly. I really want to do it this way because I am missing the database variables. Actually, you can get the number of these variables from the database, I just don’t know which variables to use. That is why I want to read it manually.

+4
source share
3 answers

If I understand you correctly, you can do this with a counter in your loop.

 <cfset counter = 0 /> <cfloop query="get_serial"> <cfif PROCESS_ID eq attributes.action_id> #SERIAL_NO# <cfset counter ++ /> </cfif> </cfloop> <cfoutput>Output #counter# times!</cfoutput> 

EDIT: answer the following question:

 <cfset counter = 0 /> <cfsavecontent variables="myContent"> <cfloop query="get_serial"> <cfif PROCESS_ID eq attributes.action_id> #SERIAL_NO# <cfset counter ++ /> </cfif> </cfloop> </cfsavecontent> <cfoutput> <p>Output #counter# times!</p> <p>#myContent#</p> </cfoutput> 

Hope this helps!

+1
source

ColdFusion provides a variable that tells you how many rows are returned in your query, without starting a loop to count them:

 <cfquery name="get_serial" datasource="#dsn3#"> SELECT * FROM SERVICE_GUARANTY_NEW WHERE STOCK_ID = #attributes.action_row_id# ORDER BY SERIAL_NO </cfquery> <cfoutput>There are #get_serial.recordCount# rows.</cfoutput> <cfoutput query="get_serial"> <p>#get_serial.serial_no#</p> </cfoutput> 
+4
source

Here the other couple answers, just for fun :), although having a counter is probably the best way, since you are already facing a performance hit when using a loop.

 <cfquery name="getCount" dbtype="query"> SELECT PROCESS_ID FROM get_serial WHERE PROCESS_ID = #attributes.action_id# </cfquery> <cfset total = getCount.RecordCount /> 

And using lists is always fun:

 <cfset total = ListValueCount(ValueList(get_serial.PROCESS_ID), attributes.action_id) /> 

The point is that there are many ways to solve this problem, so have fun :)

+1
source

Source: https://habr.com/ru/post/1342256/


All Articles