循环基于asp经典的变量值

时间:2014-11-08 17:24:03

标签: asp-classic

我必须根据变量的值输出一个表。我有一个独特的变量表块。根据视图,应该有一个循环重新读取表。

变量可以包含值两个三个或两者。在所有情况下,我必须输出包含 One 的表格。

示例#1:

Views = "Two, Three"

输出:

<table>
    <tr>
        <td>Table For View One</td>
    </tr>
</table>
<table>
    <tr>
        <td>Table For View Two</td>
    </tr>
</table>
<table>
    <tr>
        <td>Table For View Three</td>
    </tr>
</table>

示例#2:

Views = "Two"

输出:

<table>
    <tr>
        <td>Table For View One</td>
    </tr>
</table>
<table>
    <tr>
        <td>Table For View Two</td>
    </tr>
</table>

示例#3:

Views = "Three"

输出:

<table>
    <tr>
        <td>Table For View One</td>
    </tr>
</table>
<table>
    <tr>
        <td>Table For View Three</td>
    </tr>
</table>

尝试混合一些While和For,但没有成功。

<%
i=2
Do While i = 0
%>
<table>
    <tr>
        <td><% Response.Write i %></td>
    </tr>
</table>
<%
   i=i-1
Loop
%>

3 个答案:

答案 0 :(得分:0)

试试这个:

<%
Dim Views : Views ="Two"    'Change this to check

Response.write "Initial View =" & Views & "<br/>"

'But "One" will always be printed
If InStr(Views,"One")=0 Then
    'add if there is no "One" already
    Views = Views & ",One"
End If


Response.write "New View =" & Views & "<br/>"

Dim ArrValues : ArrValues =Array("One","Two","Three")

Dim Counter 

For Counter=0 to UBound(ArrValues)
    If InStr(Views,ArrValues(Counter))<>0 Then
    %>
    <table>
    <tr>
        <td>Table For View <%=ArrValues(Counter)%></td>
    </tr>
</table>
    <%
        'Response.write ArrValues(Counter) & "<br/>"
    End If
Next



%>

答案 1 :(得分:-1)

我认为这样的事情会很好。我只是使用if else if。此外,我还认为视图可以等于&#34;二,三&#34;或&#34;三,二&#34;。

<%if Views = "Two" then%>
    <table>
        <tr>
            <td>Table For View One</td>
        </tr>
    </table>
    <table>
        <tr>
            <td>Table For View Two</td>
        </tr>
    </table>
<%else if Views = "Three"%>
    <table>
        <tr>
        <td>Table For View One</td>
        </tr>
    </table>
    <table>
        <tr>
            <td>Table For View Three</td>
        </tr>
    </table>
<%else if Views = "Two, Three"%>
    <table>
        <tr>
            <td>Table For View One</td>
       </tr>
    </table>
    <table>
        <tr>
            <td>Table For View Two</td>
        </tr>
    </table>
    <table>
        <tr>
            <td>Table For View Three</td>
        </tr>
    </table>
<%else if Views = "Three, Two"%>
     <table>
        <tr>
            <td>Table For View One</td>
        </tr>
    </table>
    <table>
        <tr>
            <td>Table For View Three</td>
        </tr>
    </table>
    <table>
        <tr>
            <td>Table For View Two</td>
        </tr>
    </table>
<%end if%>

答案 2 :(得分:-2)

有多种方法可以解决这个问题,但无需更改代码,可以使用逗号Views作为分隔符将,变量拆分为数组。获得数组后,使用For循环迭代视图并使用Select语句返回正确的视图。

正如您所指定的那样,表1总是被包含在内,它已被置于循环之外。

像这样(未经测试);

<%
Dim item, items
'Build the array from the Views comma separated string
Views = Split(Views, ",")

'Always include Table One
%>
<table>
<tr>
  <td>Table For View One</td>
</tr>
</table>
<%
'Check we have an Array built from the Split().
If IsArray(Views) Then
  'How many views have we requested?
  items = UBound(Views)
  For item = 0 To items
    'What view are we current looking at in the loop?
    Select Case LCase(Trim(Views(item) & ""))
    Case "two"
%>
<table>
<tr>
  <td>Table For View Two</td>
</tr>
</table>
<%
    Case "three"
%>
<table>
<tr>
  <td>Table For View Three</td>
</tr>
</table>
<%
    End Select
  Next
End If
%>

根据视图的复杂程度,您可以将每个表分成自己的Sub过程或一个Sub过程,并代替内联表定义进行调用。像下面的东西;

Sub ShowTable(view)
  'Keep all in-line table definitions together.
  Select Case Trim(LCase(view & ""))
  Case "one"
%>
<table>
<tr>
  <td>Table For View One</td>
</tr>
</table>
<%
  Case "two"
%>
<table>
<tr>
  <td>Table For View Two</td>
</tr>
</table>
<%
  Case "three"
%>
<table>
<tr>
  <td>Table For View Three</td>
</tr>
</table>
<%
  End Select
End Sub

在主循环中,您将添加ShowTable()个调用来代替任何内联表代码,例如;

<%
Dim item, items
'Build the array from the Views comma separated string
Views = Split(Views, ",")

'Always include Table One
Call ShowTable("one")
'Check we have an Array built from the Split().
If IsArray(Views) Then
  'How many views have we requested?
  items = UBound(Views)
  For item = 0 To items
    'What view are we current looking at in the loop?
    Call ShowTable(views(item))
  Next
End If
%>

例如,您的输出将如下所示(如果您通过"Two, Three" );

<table>
<tr>
  <td>Table For View One</td>
</tr>
</table>
<table>
<tr>
  <td>Table For View Two</td>
</tr>
</table>
<table>
<tr>
  <td>Table For View Three</td>
</tr>
</table>

  

根据OP反馈进行更新

     

如果you don't want multiple tables从您的问题中未明确),则只需删除<table></table>内联定义( ShowTable()程序),这将创建&#34; guts&#34;您的表(一系列表行)。   然后在上面的示例代码中,只需在开始时和代码<table>之后添加</table>,它将动态生成的表行封装到一个表中。

     
<table>
<%
Dim item, items
'Build the array from the Views comma separated string
Views = Split(Views, ",")

'Always include Table One
Call ShowTable("one")
'Check we have an Array built from the Split().
If IsArray(Views) Then
  'How many views have we requested?
  items = UBound(Views)
  For item = 0 To items
    'What view are we current looking at in the loop?
    Call ShowTable(views(item))
  Next
End If
%>
</table>
     

例如,您的输出将如下所示(如果您通过"Two, Three" );

     
<table>
<tr>
  <td>Table For View One</td>
</tr>
<tr>
  <td>Table For View Two</td>
</tr>
<tr>
  <td>Table For View Three</td>
</tr>
</table>