Link to home
Start Free TrialLog in
Avatar of Coast Line
Coast LineFlag for Canada

asked on

iterating over array data

hi, i am using the following query to get the results in the following format

<cffunction access="public" name="getSettings" returntype="array">
  	<cfset var mySettings = "">
	<cfquery name="mySettings">
      EXEC sp_site_Mail_Templates;
    </cfquery>
	<cfreturn QueryToArray(mySettings)>
</cffunction>

Open in new window


using the querytoarray function to get the results in an array


<cffunction name="QueryToArray" access="public" returntype="array" output="false" hint="This turns a query into an array of structures.">
  <!--- Define arguments. --->
  <cfargument name="Data" type="query" required="yes" />
  <cfscript>    
    var LOCAL = StructNew(); // Define the local scope.
    LOCAL.Columns = data.getMetaData().getColumnLabels(); // Get the column names as an array.
    LOCAL.QueryArray = ArrayNew(1); // Create an array that will hold the query equivalent.
    for (LOCAL.RowIndex = 1 ; LOCAL.RowIndex LTE ARGUMENTS.Data.RecordCount; 
	LOCAL.RowIndex = (LOCAL.RowIndex + 1)){
    	LOCAL.Row = StructNew();
    	for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE ArrayLen(LOCAL.Columns); 
		LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){
    		LOCAL.ColumnName = LOCAL.Columns[LOCAL.ColumnIndex];
    		LOCAL.Row[LOCAL.ColumnName] = ARGUMENTS.Data[LOCAL.ColumnName][LOCAL.RowIndex];
    	}
    	ArrayAppend(LOCAL.QueryArray, LOCAL.Row);
    }
    return(LOCAL.QueryArray);
    </cfscript>
</cffunction>

Open in new window


now i am trying to use in cfoutput or cloop to fill in my my textareas, i am stuck as to why it is not going

i am trying to use array like this
<cfloop from="1" to="#arrayLen(rsSettings)#" index="i">
<cfoutput>
<form action="#cgi.SCRIPT_NAME#?#cgi.QUERY_STRING#" method="post" id="formSettings" name="formSettings" class="formSettings">
<fieldset><legend>Configure Mail Templates</legend>
  <div class="onleft">
   <label>Account <span>{NAME}, {SUBJECT}, {BODY}, {CREATED}, {LINK}, {PRIORITY}, {DEPARTMENT}</span></label>
    <textarea name="tce" rows="10" class="textareas" cols="50">#data['log']#</textarea>
    <label>Account Updated Email <span>{NAME}, {CREDENTIALS}, {CREATED}</span></label>
    <textarea name="acc" rows="10" class="textareas" cols="50">#data['reg']#</textarea>
    <label>Order Email <span>{NAME}, {CREATED}, {BYWHOM}, {CANNEDMSG}</span></label>
    <textarea name="can" rows="10" class="textareas" cols="50">#data['ord']#</textarea>
  </div>
</form>
</cfoutput>
</cfloop>

Open in new window


but it is not entering the values properly
Avatar of gdemaria
gdemaria
Flag of United States of America image

I don't see how the array / structure in the function matches the structure "data" in the last code post.

I think you should do a cfdump of the value returned from the function and see what you get.    It looks like the format you are creating is : data[RecordNumber][ColumnName]

In your code, you are using data['ord']  with no record/array number

You are looping :  
<cfloop from="1" to="#arrayLen(rsSettings)#" index="i">

rsSettings and then using #data#

you should use  data[ i ]['ord']


Btw, my personal pet peeve is using a single letter for index (such as i ) because it's impossible to search for.   I suggest using  ii   or  jj   or  something like  "aValue"
Avatar of Coast Line

ASKER

here is the screenshot, how it needs to be looking
Capture.PNG
Thank you for the dump image,  my assumption was correct, the first element is the number which you were not using.

Your reference of the structure needs to start with the array position as the image shows... something like

     data[ i ]['ord']
can you post a complete code
Looking at your image, please tell me which values that you want to use.  For example, I see the value "tce" and "tue" under the "name" element.   Are those the values you want to use?
yes, if i call tce, it should call the html_body in the textarea and i need to save them too in the database after a change
I hope this will help, I don't know what your code is about, but this is how to loop through that structure.
I don't know why your CFLOOP used RSSETTING, so I am using DATA as the variable.  I assume the image is a dump of #data#...

<cfoutput>
<cfloop from="1" to="#arrayLen(data)#" index="kk">
    <cfset theStruct = data[kk]>
    <cfif Struct['Name'] is "tce">
	  Found tce: #struct['html_body']#<br>
	<cfelse>
	  Other: #struct['name']# is #struct['html_body']#<br>
	</cfif>
</cfloop>   
</cfoutput>

Open in new window

its making multiple textareas when i am looping over, why it is happening

<cfloop from="1" to="#arrayLen(data)#" index="i">
<cfset theStruct = data[i]>
  <div class="onleft">
   <label>Account Registration<span></span></label>
    <textarea name="tce" rows="10" class="textareas" cols="50"><cfif theStruct['Name'] is "tce">#theStruct['html_body']#</cfif></textarea>
    <label>Account Updated Email <span></span></label>
     <textarea name="acc" rows="10" class="textareas" cols="50"><cfif theStruct['Name'] is "tue">#theStruct['html_body']#</cfif></textarea>
    <label>Order Email <span></span></label>
    <textarea name="can" rows="10" class="textareas" cols="50"><cfif theStruct['Name'] is "nar">#theStruct['html_body']#</cfif></textarea>
  </div>
  <div class="onarse">
    <label>New Account Registration <span>{NAME}, {CREDENTIALS}, {DATE}</span></label>
    <textarea name="nar" class="textareas" cols="50"><cfif theStruct['Name'] is "fpe">#theStruct['html_body']#</cfif></textarea>
    <label>Forgot Password Activation <span></span></label>
    <textarea name="fpe" class="textareas" cols="50"><cfif theStruct['Name'] is "pae">#theStruct['html_body']#</cfif></textarea>
    <label>Password Reset <span>{NAME}, {CREDENTIALS}, {CREATED}</span></label>
    <textarea name="pae" class="textareas" cols="50"><cfif theStruct['Name'] is "acc">#theStruct['html_body']#</cfif></textarea>
    <label>Updated Email <span></span></label>
    <textarea name="tue" class="textareas" cols="50"><cfif theStruct['Name'] is "can">#theStruct['html_body']#</cfif></textarea>
  </div>
  <br class="clr" />
  <label></label><br class="clr"/>
</cfloop>

Open in new window

You don't have anything to NOT create the text areas.   You have about 7 textarea tags for every iteration of the loop.   If you don't want all seven created each loop, then you have to have some code to not create that box.    What do you expect the final result to be? Maybe you don't want to loop at all?
no i don't want that

let's do this way, here is the result of the query what i am getting, leave all arrays and structures behind, now i want that for every record, it should have its entry in its own textarea, please find the attached image
Capture.PNG
Capture2.PNG
Ok, just to be sure I understand..
The image shows  9 records, but each record has a different value in the "name" column.   What you want is ONE textarea field for every value in the name column.

<textarea name="tce"> Hello.....</textarea>

<textarea name="tue">Hello.... </textarea>

Is that right?  So for this example, you want 9 text area fields the content is HTML_BODY and the name of the text area is the value in the NAME field (tce, tue, var, etc...)

Did I get it right?
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks