如何循环ASP中的每个Recordset

时间:2013-07-01 03:54:44

标签: ms-access asp-classic

我需要循环帮助,我尝试使用子菜单创建菜单,在我的数据库中,我有一个名为tblMenu的表,它有五个字段( link_position 链接 link_name link_title sub_menu

link_position 字段用于排序菜单项的位置,

link 字段存储菜单项

的链接

link_name 包含菜单项的名称,

link_title 包含菜单项的链接标题,

sub_menu 存储菜单的子项目(下拉列表)。

以下是tblMenu的预览

enter image description here

以下是循环代码

<ul>
<%
msql = "SELECT * FROM tblMenu ORDER BY [link_position] ASC"
Set RS = Server.CreateObject("ADODB.Recordset")
Set RSsub = Server.CreateObject("ADODB.Recordset")
RS.Open msql, Conn, 1,3
SET RSsub = Conn.execute ("SELECT * FROM tblMenu ORDER BY [link_position] ASC")

If Not RS.EOF Then
Do
Response.Write("<li><a href=""" & RS("link") & """ title =""" & RS("link_title") & """>" & RS("link_name") & "</a>" & vbcrlf)

            If Not RSsub.EOF Then
                Response.Write("<ul class=""sub-menu"">" & vbcrlf)
                Do While Not RSsub.EOF
                    Response.Write(RSsub("sub_menu") & vbcrlf)
                    RSsub.MoveNext()
                Loop
                Response.Write "</ul>" & vbcrlf
            End If
'                RSsub.Close

                Response.Write("</li>" & vbcrlf)
                RS.MoveNext()

        Loop Until RS.EOF
    End If          
%>
</ul>

输出

<ul>
<li><a href="/asp/" title ="Main Page">Home</a>
<ul class="sub-menu">

<li><a href="#">Inquiry</a></li>
<li><a href="#">Inquiry2</a></li>
<li><a href="#">ELS</a></li>
<li><a href="#">AP</a></li>
</ul>
</li>

<li><a href="/asp/page.asp?pID=1" title ="About us">About</a></li>
<li><a href="/asp/page.asp?pID=2" title ="E-Learning System">E-Learning</a></li>
<li><a href="/asp/page.asp?pID=5" title ="Academic Policies">Academic Policies</a></li>
<li><a href="/asp/page.asp?pID=6" title ="Facilities">Facilities</a></li>
<li><a href="/asp/events.asp" title ="Events Gallery">Events</a></li>

</ul>

问题

sub_menus的循环抛出Main循环的第一个Recordset下的所有记录,我期望的输出应该是这样的

<ul>
<li><a href="/asp/" title ="Main Page">Home</a></li>

<li><a href="/asp/page.asp?pID=1" title ="About us">About</a>
<ul class="sub-menu">
<li><a href="#">Inquiry</a></li>
<li><a href="#">Inquiry2</a></li>
</ul>
</li>

<li><a href="/asp/page.asp?pID=2" title ="E-Learning System">E-Learning</a>
<ul class="sub-menu">
<li><a href="#">ELS</a></li>
</ul>
</li>

<li><a href="/asp/page.asp?pID=5" title ="Academic Policies">Academic Policies</a>
<ul class="sub-menu">
<li><a href="#">AP</a></li>
</ul>
</li>

<li><a href="/asp/page.asp?pID=6" title ="Facilities">Facilities</a></li>
<li><a href="/asp/events.asp" title ="Events Gallery">Events</a></li>

</ul>

1 个答案:

答案 0 :(得分:5)

我不确定为什么你两次运行相同的查询。您似乎从第一个SQL中获得了所需的所有信息。如果我理解你的问题是正确的,那么这个代码既简单又快捷。

<%
msql = "SELECT link_position, link, link_name, link_title, IIF(IsNull(sub_menu),'',sub_menu) as sub_menu FROM tblMenu ORDER BY [link_position] ASC"
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open msql, Conn, 1,3

If Not RS.EOF Then
    response.write "<ul>"
    Do
        Response.Write("<li><a href='" & RS("link") & "' title ='" & RS("link_title") & "'>" & RS("link_name") & "</a>" & vbcrlf)

        If Not trim(RS("sub_menu")) = "" Then
            Response.Write("<ul class='sub-menu'>" & vbcrlf)
            Response.Write(RS("sub_menu") & vbcrlf)
            Response.Write "</ul>" & vbcrlf
        End If
        Response.Write("</li>" & vbcrlf)
        RS.MoveNext()
    Loop Until RS.EOF
    response.write "</ul>"
End If          
%>