Skip to main content
Inspiring
January 18, 2012
Question

Adding to Array problem

  • January 18, 2012
  • 3 replies
  • 1297 views

Hi All,

I have a form which the user can upload a document to a location on my server.  Basically everytime the user uploads a document I would like to add the information into an array so I can store the information in memory and then when the user clicks on the save button I will save away the information in the array.   I am having a slight issue when inserting a row into the array, it seems to work on the intial first time but when the user tries to upload a second document only the current information shows in the last row of the array and wiping out anything before that.

Any help would be much apprciated I have attached my code in the hope someone can.


Many thanks,
George

<cfparam name="form.document" type="string" default="" />

<cfparam name="form.submitted" type="string" default="0" />

<cfparam name="form.ArLen" type="string" default="0">

<cfset REQUEST.Errors = ArrayNew(1) />

<!--- Create 2D array --->

<cfif isdefined("DocumentsArray") or (form.submitted neq 0)>

          <cfset DocumentsArray = ArrayNew(2)>

</cfif>

<cfset tDate = "#dateformat(now(), "yyyymmdd")#">

<cfset tTime = "#timeformat(now(), "HHmmss")#">

<cfif Val(FORM.submitted)>

          <cfif NOT Len(FORM.document)>

                    <cfset ArrayAppend(REQUEST.Errors,"Please select a file to upload.") />

          </cfif>

          <cfif NOT ArrayLen(REQUEST.Errors)>

                    <!--- Upload document --->

                     <cffile action="UPLOAD" destination="#ExpandPath('#get_serversettings.root_data_path#')#" filefield="document" nameconflict="MAKEUNIQUE"/>

                    <cfset tFileLoc = #get_serversettings.root_data_path# & #tFileName# & "." & #CFFILE.clientFileExt#>

                    <cfset tSourceLoc = #get_serversettings.root_data_path# & #CFFILE.ServerFile#>

                    <!--- **************************** --->

                    <!--- STEP 1: Build array contents --->

                    <!--- **************************** --->

                    <cfif isdefined("form.ArLen")>

                              <cfset form.ArLen = #form.ArLen# + 1>

                    </cfif>

 

                    <cfset DocumentsArray[#form.ArLen#][1] = #UAC#>

                    <cfset DocumentsArray[#form.ArLen#][2] = #AppealRef#>

                    <cfset DocumentsArray[#form.ArLen#][3] = now()>

                    <cfset DocumentsArray[#form.ArLen#][4] = #CFFILE.ServerFile#>

                    <cfset DocumentsArray[#form.ArLen#][5] = #tFileName#>

          </cfif>

</cfif>

<cfoutput>

          <fieldset>

                    <legend>Upload Document</legend>

                    <cfif ArrayLen(REQUEST.Errors)>

                              <ul>

                                        <cfloop index="intError" from="1" to="#ArrayLen(REQUEST.Errors)#" step="1">

                                                   <li>#REQUEST.Errors[intError]#</li>

                                         </cfloop>

                              </ul>

                    <cfelse>

                              <ul>

                                        <cfif Val(FORM.submitted)>

                                                  <li>File uploaded.</li>

                                        </cfif>

                              </ul>

                     </cfif>

                     <form action="#CGI.script_name#" method="post" enctype="multipart/form-data">

                              <input type="hidden" name="submitted" value="1"/>

                              <input type="hidden" name="ArLen" value="#form.ArLen#">

 

                              <label>Select a file:</label>

                              <input type="file" name="document" value="" size="50" /><br /><br />

 

                              <input type="submit" value="Upload" />

                    </form>

          </fieldset>

</cfoutput>

<cfoutput>

          <cftry>

                    <cfdump var=#DocumentsArray#>

          <cfcatch></cfcatch>

          </cftry>

</cfoutput>

    This topic has been closed for replies.

    3 replies

    Inspiring
    February 9, 2012

    Just out of curiousity, what does the array get used for?

    February 9, 2012

    Hi George,

    Rather than using a value for the length of the array, have a look at the arrayAppend() function which automatically appends a value to the end of an array (or  arrayPrepend() if you want to place the value at the start of an array).

    Cheers

    Andy

    BKBK
    Community Expert
    Community Expert
    January 18, 2012

    What is Arlen for? My suggestions follow. Hopefully they speak for themselves.

    <cfparam name="variables.isFileUploaded" type="boolean" default="FALSE">

    <!--- Create 2D array in the user's session, if it doesn't exist. (The session scope persists the array. If it were in the variables scope, it would be re-initialized at every request) --->

    <cfparam name="session.DocumentsArray" default="ArrayNew(2)">

    <cfset REQUEST.Errors = ArrayNew(1) />

    <cfset tDate = "#dateformat(now(), "yyyymmdd")#">

    <cfset tTime = "#timeformat(now(), "HHmmss")#">

    <cfif isDefined("form.document")><!--- Then form has been submitted --->

        <!--- Create local copy of arLen for use on this page --->

        <cfparam name="variables.ArLen" type="string"  default="#trim(form.arLen)#">

        <cfif NOT Len(trim(FORM.document))>

            <cfset ArrayAppend(REQUEST.Errors,"Please select a file to upload.") />

        </cfif>

       

        <cfif NOT ArrayLen(REQUEST.Errors)>

            <cftry><!--- Use try/catch to catch any other errors, for example, file fails to upload, Arlen is non numeric, etc.--->

                <!--- Upload document --->

                <cffile action="UPLOAD" destination="#ExpandPath('#get_serversettings.root_data_path#')#" filefield="document" nameconflict="MAKEUNIQUE"/>

                <cfset variables.isFileUploaded  = true>

                <cfset tFileLoc = #get_serversettings.root_data_path# & #tFileName# & "." & #CFFILE.clientFileExt#>

                <cfset tSourceLoc = #get_serversettings.root_data_path# & #CFFILE.ServerFile#>

               

               

                <!--- **************************** --->

               

                <!--- STEP 1: Build array contents --->

               

                <!--- **************************** --->

               

                <cfset variables.ArLen = variables.ArLen + 1>

                           

                <cfset session.DocumentsArray[variables.ArLen][1] = #UAC#>

                <cfset session.DocumentsArray[variables.ArLen][2] = #AppealRef#>

                <cfset session.DocumentsArray[variables.ArLen][3] = now()>

                <cfset session.DocumentsArray[variables.ArLen][4] = #CFFILE.ServerFile#>

                <cfset session.DocumentsArray[variables.ArLen][5] = #tFileName#>

            <cfcatch type="any">

                <cfset ArrayAppend(REQUEST.Errors,cfcatch.message) />

            </cfcatch>

            </cftry>

        </cfif>

    </cfif>

    <cfoutput>

              <fieldset>

                        <legend>Upload Document</legend>

                        <cfif ArrayLen(REQUEST.Errors)>

                                  <ul>

                                            <cfloop index="intError" from="1" to="#ArrayLen(REQUEST.Errors)#" step="1">

                                                       <li>#REQUEST.Errors[intError]#</li>

                                             </cfloop>

                                  </ul>

                         </cfif>

                 <!--- A separate condition, as some errors might still allow the file to be uploaded --->

                 <cfif variables.isFileUploaded>

                                  <ul>                          

                                      <li>File uploaded.</li>

                                  </ul>

                         </cfif>

                         <form action="#CGI.script_name#" method="post" enctype="multipart/form-data">

                                  <input type="hidden" name="ArLen" value="#variables.ArLen#">

                                  <label>Select a file:</label>

                                  <input type="file" name="document" value="" size="50" /><br /><br />

                                  <input type="submit" name="sbmt" value="Upload" />

                        </form>

              </fieldset>

    </cfoutput>

    <cfdump var=#session.DocumentsArray#>

    _G_1Author
    Inspiring
    January 18, 2012

    ArLen was going to be my array length flag so I could add to different positions in the array.

    Many thanks for your suggestions, I have implemented it on my form but I am receiving the following error:

    • You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

    It seems to be pointing to this line of code:  <cfset session.DocumentsArray[variables.ArLen][1] = #UAC#>

    Any ideas ?

    BKBK
    Community Expert
    Community Expert
    January 20, 2012

    \'G\' wrote:

    ArLen was going to be my array length flag so I could add to different positions in the array.

    Many thanks for your suggestions, I have implemented it on my form but I am receiving the following error:

    • You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

    It seems to be pointing to this line of code:  <cfset session.DocumentsArray[variables.ArLen][1] = #UAC#>

    Any ideas ?

    Sorry about that. My initialization of variables.Arlen and session.DocumentsArray is wrong. The following attempt is better.

    <cfparam name="variables.isFileUploaded" type="boolean" default="FALSE">

    <!--- Initialize local copy of arLen for use on this page --->

    <cfparam name="variables.arLen" type="integer" default="1">

    <!--- Create 2D array in the user's session, if it doesn't exist. (The session scope persists the array. If it were in the variables scope, it would be re-initialized at every request) --->

    <cfif NOT isDefined("session.DocumentsArray")>

        <cfset session.DocumentsArray = ArrayNew(2)>

    </cfif>

    <cfset REQUEST.Errors = ArrayNew(1) />

    <cfset tDate = dateformat(now(), "yyyymmdd")>

    <cfset tTime = timeformat(now(), "HHmmss")>

    <cfif isDefined("form.document")><!--- Then form has been submitted --->  

       <cfset variables.ArLen = trim(form.arLen)>

        <cfif NOT Len(trim(FORM.document))>

            <cfset ArrayAppend(REQUEST.Errors,"Please select a file to upload.") />

        </cfif>

        <cfif NOT ArrayLen(REQUEST.Errors)>

            <cftry><!--- Use try/catch to catch any other errors, for example, file fails to upload, Arlen is non numeric, etc.--->

                <!--- Upload document --->

                <cffile action="UPLOAD" destination="#ExpandPath('#get_serversettings.root_data_path#')#" filefield="document" nameconflict="MAKEUNIQUE"/>

                <cfset variables.isFileUploaded  = true>

                <cfset tFileLoc = #get_serversettings.root_data_path# & #tFileName# & "." & #CFFILE.clientFileExt#>

                <cfset tSourceLoc = #get_serversettings.root_data_path# & #CFFILE.ServerFile#>

                <!--- **************************** --->   

                <!--- STEP 1: Build array contents --->

                <!--- **************************** --->

                <cfset variables.ArLen = variables.ArLen + 1>

                <cfset session.DocumentsArray[variables.ArLen][1] = #UAC#>

                <cfset session.DocumentsArray[variables.ArLen][2] = #AppealRef#>

                <cfset session.DocumentsArray[variables.ArLen][3] = now()>

                <cfset session.DocumentsArray[variables.ArLen][4] = #CFFILE.ServerFile#>

                <cfset session.DocumentsArray[variables.ArLen][5] = #tFileName#>

            <cfcatch type="any">

                <cfset ArrayAppend(REQUEST.Errors,cfcatch.message) />

            </cfcatch>

            </cftry>

        </cfif>

    </cfif>

    <cfoutput>

              <fieldset>

                        <legend>Upload Document</legend>

                        <cfif ArrayLen(REQUEST.Errors)>

                                  <ul>

                                            <cfloop index="intError" from="1" to="#ArrayLen(REQUEST.Errors)#" step="1">

                                                       <li>#REQUEST.Errors[intError]#</li>

                                             </cfloop>

                                  </ul>

                         </cfif>

                 <!--- A separate condition, as some errors might still allow the file to be uploaded --->

                         <cfif variables.isFileUploaded>

                                  <ul>                         

                                      <li>File uploaded.</li>

                                  </ul>

                         </cfif>

                         <form action="#CGI.script_name#" method="post" enctype="multipart/form-data">

                                  <input type="hidden" name="ArLen" value="#variables.ArLen#">

                                  <label>Select a file:</label>

                                  <input type="file" name="document" value="" size="50" /><br /><br />

                                  <input type="submit" name="sbmt" value="Upload" />

                        </form>

             </fieldset>

    </cfoutput>

    <cfdump var="#session.DocumentsArray#">