How can I iterate over a query a certain number of times, which may be more than the result?

I need to execute the request loop exactly 12 times to fill in the lines in the form, but rarely the request will return 12 lines. The cfquery endRow attribute does not make the loop continue if the result is <12. If that were the case, it would be ideal to use something like cfloop query = "myQuery" endRow = "12" ... 2 options that I have Now, it is to skip the loop and write out all 12 lines, but this leads to a large duplicated code (there are 20 columns) or to make a query request for each line, which seems like a lot of wasted processing. Thanks for any ideas.

+4
source share
2 answers

You can just use

maxrows="12" 

Although, I think that something may be wrong with your logic. maybe if you post some of our code, I can take a look at the suggested approach.

maxRows will do the trick though

UPDATE

Recall that maxrows must be used with the "cfoutput query" because the cfloop query does not support it.

In this case, you will do something like:

 <cfoutput query="myQuery" maxRows="12"> 

UPDATE UPDATE

Realizing what exactly you wanted, I wrote the following code, which, in my opinion, you need:

 <cfscript> qryTest = QueryNew("name,email"); newRows = QueryAddRow(qryTest,5); tmp = querySetCell(qryTest, 'name', 'John', 1); tmp = querySetCell(qryTest, 'email', ' John@email.com ', 1); tmp = querySetCell(qryTest, 'name', 'Paul', 2); tmp = querySetCell(qryTest, 'email', ' Paul@bob.com ', 2); tmp = querySetCell(qryTest, 'name', 'George', 3); tmp = querySetCell(qryTest, 'email', ' George@bob.com ', 3); tmp = querySetCell(qryTest, 'name', 'Ringo', 4); tmp = querySetCell(qryTest, 'email', ' Ringo@bob.com ', 4); tmp = querySetCell(qryTest, 'name', 'Yoko', 5); tmp = querySetCell(qryTest, 'email', ' Yoko@bob.com ', 5); </cfscript> <cfdump var="#qryTest#"> <form name="test"> <cfoutput> <cfloop from="1" to="12" index="ii"> <cfif ii GT qryTest.recordCount> <cfset tmp = QueryAddRow( qryTest, ii)> </cfif> Name: <input type="text" name="name_#ii#" value="#qryTest.name[ii]#"><br /> Wmail: <input type="text" name="email_#ii#" value="#qryTest.email[ii]#"><br /><br /> </cfloop> </cfoutput> </form> <cfdump var="#qryTest#"> 

This will add new lines to your query dynamically if necessary (i.e. if you do not have 12 lines in your recordset)

It imitates a set of records so you can copy and paste the code and see the results.

hope this helps; -)

+2
source

If you don't need column values, you can try something like this ...

  <cfquery NAME="testQuery" datasource="#DB#" > SELECT SOMETHING FROM SOMETHING </cfquery> <cfif testQuery.recordcount LT 12> <cfset temp = QueryAddRow( testQuery, 12- testQuery.recordcount)> </cfif> 
+1
source

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


All Articles