使用while循环ASP vbscript创建一个函数?

时间:2016-08-19 23:42:47

标签: function vbscript asp-classic recordset

我使用field.name和field.value直接从ADO记录集创建表。从另一篇先前的帖子Retrieve ADO Recordset Field names (Classic ASP)可以看出 更进一步,我试图根据另一个数据库中的值重命名表头。基本上如果th = Column_1然后写“名字”等

我可以使用特定代码的“if then”功能单独执行此操作。但是,我有另一个数据库,它具有所有正确的标题名称,而宁愿通过记录集使用while循环。而不是写每一行 - columnheading1 = x和columnheading2 = y等我宁愿创建一个while循环。

同样,单个“if then”语句在代码中如下所述正常工作,但记录集却没有 我试图修复记录集和循环,但没有好处。为什么while循环(或重复区域)在函数中不起作用的任何想法?

以下是示例:

    <table>
      <thead>
          <tr>

    <%
    Function Header_Name_Sub(Header_NameX)
    Header_Name_Write = Header_NameX
    If Header_Name_Write = "Project_File_ID" Then
    Header_Name_Write = "File ID" ' this works great.
    End If
    If Header_Name_Write = "Project_Assignment_Name" Then
    Header_Name_Write = "Project Name" ' this works great.
    End If

    ' Have a data base of all header names based on column name ... this repeat is not working in the function.  If I pull it out this section works fine elsewhere in the page...
Do While Not RS_Build_Numeric_Names.EOF
    If CStr(Header_Name_Write) = CStr((RS_Header.Fields.Item("True_Column_Name").Value)) Then ' thought maybe string issue is why CStr
    Header_Name_Write = (RS_Build_Numeric_Names.Fields.Item("Fixed_Column_Name").Value
    End If
Loop

    If Header_Name_Write = "Project_City" Then
    Header_Name_Write = "City" ' this works great.
    End If
    End Function
    %>      

             <%For Each fld in RS.Fields%>
            <% 
             Header_Name_Sub(fld.Name)
             %>
    <th><span><%=Header_Name_Write%></span></th>
             <%Next %>
          </tr>
      </thead>
       <tbody>
     <%
       Do Until RS.EOF
          OutputRow RS.Fields
          RS.MoveNext
       Loop
     %>
       </tbody>
    </table>
    <%
     Sub OutputRow(fields)
    %>
          <tr>
             <%For Each fld in fields%>
               <td><span><%=(fld.Value)%></span></td>
             <%Next %>
          </tr>
     <%
     End Sub
     %>

所以这是我刚刚意识到的简单形式的编辑。在功能......

If Header_Name_Write = "Project_File_ID" Then
Header_Name_Write = "File ID" ' this works great.
End If

以上作品很棒。我试图使用while循环来编写50多个额外的'If Then',而不是它只是循环。那么如何在函数中写下额外的?

If x = 1 Then Header_Name_Write = "File ID" End If
If x = 2 Then Header_Name_Write = "Next Header" End If
If x = 3 Then Header_Name_Write = "Another Header" End If

它是for循环吗?

再往前走......

For Each fld in RS_Build_Numeric_Names.Fields
If Header_Confirm = (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Column_Name").Value) Then 
Header_Name_Write = (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Name").Value)
End If
Next

如果我使用for statment,那么仅对记录集中的第一条记录起作用...而不是整个循环。

3 个答案:

答案 0 :(得分:0)

因为Header_Name_Sub()是一个函数,所以你应该让它正确地返回结果,而不是使用Header_Name_Write变量。在您的代码中,Header_Name_Write应该是一个全局变量,但由于您未明确声明使用DIM语句,因此可能会产生意外结果。

你应该尝试下面的代码:

<%
  Function Header_Name_Sub(Header_NameX)
    Dim Header_Name_Write '' make it local here
    Header_Name_Write = Header_NameX
    .
    .
    .
    .
    Header_Name_Sub = Header_Name_Write '' assign the local value and return it
  End Function

  For Each fld in RS.Fields
    %><th><span><%=Header_Name_Sub(fld.Name)%></span></th><%
  Next
.
.
.

答案 1 :(得分:0)

将其计算出99%。是否正确使用脚本字典来定义标题名称。

<table border="1px">
  <thead>
      <tr>

<%
Set Header_Options_All = Server.CreateObject("Scripting.Dictionary")
Do While Not RS_Header_Options_2.eof 'one RS Loop
    Header_Options_All.Add (RS_Header_Options_2.Fields.Item("Build_Project_Drop_Column_Name").Value), (RS_Header_Options_2.Fields.Item("Build_Project_Drop_Option_Name").Value)
    RS_Header_Options_2.movenext
Loop
Do While Not RS_Build_Numeric_Names.eof 'Another RS Loop
    Header_Options_All.Add (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Column_Name").Value), (RS_Build_Numeric_Names.Fields.Item("Build_Project_Metric_Name").Value)
    RS_Build_Numeric_Names.movenext
Loop
Header_Options_All.Add "Project_File_ID", "File ID" 'singles I added
Header_Options_All.Add "Project_ID", "ID"
Header_Options_All.Add "Project_Assignment_Name", "Project Name"
Header_Options_All.Add "Project_City", "City"
%>

<%
Function Header_Name_Sub(Header_NameX)
Dim Header_Name_Write
Dim Header_Confirm
Header_Name_Write = Header_NameX
Header_Confirm = Header_NameX 
For Each elem In Header_Options_All
Header_Name_Write = Header_Options_All.Item(Header_Confirm)
Next
Header_Name_Sub = Header_Name_Write
End Function
%>      

         <%For Each fld in RS_Full_List_Building_Inner_Join.Fields%>
<th><span><%=Header_Name_Sub(fld.Name)%></span></th>
         <%Next %>
      </tr>
  </thead>

现在我无法让其他人填写。意思是如果它与名称不匹配我想要默认名称。足够近了!

答案 2 :(得分:0)

  

我突然意识到为什么循环不起作用。因为它不断重新定义循环中的值。 Doooop!我希望循环将函数写入所有需要的'if then'语句...而不是替换值。我怎么能做到这一点? -

变量=变量&amp; “新数据”