3

我正在自定义 CSV 上传器以允许列映射。我有一个将其解析为数组的函数,然后我循环遍历它以生成带有上传文件列名的选择框,以映射到我的数据库中相应表中的现有列名。

我可以让选择下拉列表填充列名,但我不知道如何循环通过上传文件中的列数量来限制选择下拉列表的总数。

解析后的函数:

<!--- Get Value from Array based on Column Name --->
<cffunction name="ValByColName" access="public" returntype="string" output="false">
    <cfargument name="ColName"type="string" required="true" default=""/>
    <cfargument name="DataArray" type="array" required="true" default=""/>
    <cfset findValue = keyArray.indexOf(#ColName#) + 1>
    <cfreturn(#DataArray[findValue]#) /> 
</cffunction>

环形:

<!--- Set Uploaded file to Array --->
                <cfset arrCSV = CSVToArray(CSVFilePath = #form.UploadedFile#,Delimiter = ",",Qualifier = """") />
                <!--- Create Key array from column names --->
                <cfset keyArray = ArrayNew(1)>
                <cfloop from="1" to="#DON'T KNOW WHAT TO PUT HERE" index="t">
                    <!--- Variable Headers --->
                    <cfif Len(form.UploadedFile) GTE 5>
                        <cftry>

                            <select name="HeaderID" class="search" id="Header">
                            <option selected value="">--- Headers Uploaded ---</option>
                            <cfoutput>
                            <cfloop from="1" to="1" index="i">

                                <cfloop from="1" to="#ArrayLen(arrCSV[i])#" index="j">

                                     <option name="HeaderID" value="#j#">#arrCSV[i][j]#</option>

                                </cfloop>
                            </cfloop>
                            </cfoutput>
                            </select> =
                    </cftry> 

                    </cfif>
                    <cfquery name="clientsCols" datasource="#request.dsn#">
                        select Column_name 
                        from Information_schema.columns 
                        where Table_name like 'Clients'
                    </cfquery>
                    <!--- Constants--->
                    <cfif Len(form.UploadedFile) GTE 5>
                        <cftry>
                            <select name="ColumnID" class="search" id="Column">
                            <option selected value="">--- Headers Clients ---</option>
                            <cfoutput>
                                <cfloop query="clientsCols">
                                <option name="ColumnID" value="#Column_name#">#Column_name#</option>
                                </cfloop>
                            </cfoutput>
                            </select><br /><br />
                    </cftry> 

                    </cfif>
                </cfloop>
4

1 回答 1

1

如果您将其与上面的原始版本进行比较,您会发现 usingcfloop from="1" to="#ArrayLen(arrCSV[1])#" index="t"正是我所需要的(解决方案中的第 5 行)。上传后,选择框对的数量就是上传文件中列标题的数量。

感谢大家的回复。

<!--- Set Uploaded file to Array --->
<cfset arrCSV = CSVToArray(CSVFilePath = #form.UploadedFile#,Delimiter = ",",Qualifier = """") />
<!--- Create Key array from column names --->
<cfset keyArray = ArrayNew(1)>
<cfloop from="1" to="#ArrayLen(arrCSV[1])#" index="t">
    <!--- Variable Headers --->
    <cfif Len(form.UploadedFile) GTE 5>
        <select name="HeaderID" class="search" id="Header">
            <option selected value="">--- Headers Uploaded ---</option>
            <cfoutput>
            <cfloop from="1" to="1" index="i">
                <cfloop from="1" to="#ArrayLen(arrCSV[i])#" index="j">
                    <option name="HeaderID#t#" value="#j#">#arrCSV[i][j]#</option>
                </cfloop>
            </cfloop>
            </cfoutput>
        </select> =
    </cfif>
    <!---Column Constants--->
    <select name="ColumnID" class="search" id="Column">
        <option selected value="">--- Headers Clients ---</option>
        <cfoutput>
        <cfloop query="clientsCols">
            <option name="ColumnID#T#" value="#Column_name#">#Column_name#</option>
        </cfloop>
        </cfoutput>
    </select><br /><br />
</cfloop>
于 2013-06-04T18:23:30.477 回答