日期:2014-05-17  浏览次数:20856 次

ASP列循环的实现,谁会?
一般情况下是这样循环的:
姓名 年级 性别
张三 1 男
李四 3 女
王五 2 男

要求这样实现:
姓名 张三 李四 王五
年级 1 3 2
性别 男 女 男  

上面就是传说中的"ASP无敌列循环"!

------解决方案--------------------
希望给你些提示


1.首先取得所有课程,并生成表头
2.按学号、课程排序,把部分按表头的顺序填入表格。
代码如下:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Option Explicit 
Dim connstr, rs, sql, conn, i, j
dim kcs(10), kccount '课程名,课程数
connstr = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" & Server.MapPath("board.mdb")
set conn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.Recordset")
conn.open connstr

sql = "select distinct 课程 from 成绩 order by 课程"
rs.open sql, conn

kccount = -1 
while not rs.eof 
kccount = kccount + 1
kcs(kccount) = rs(0)
rs.movenext 
wend 
rs.close

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<table border="1"><tr><th>学号</th>
<%
''表头
for i=0 to kccount 
response.Write("<th>" & kcs(i) & "</th>")
next 
%>
</tr>

<%
sub endline
if lastxh <> "" then 
''如果不是所有的课程都已经有成绩,则补全没有成绩的课程
for j=i to kccount 
response.Write("<td> </th>")
next 
response.Write("</tr>")
end if 
end sub

''表身,多笔记录
sql = "select * from 成绩 order by 学号, 课程"
rs.open sql, conn

dim lastxh
lastxh = "" ''正常的学号不为空
do while not rs.eof 
if lastxh <> rs("学号") then 
''结束旧一行
endline

lastxh = rs("学号") 
''开始一个新行
response.Write("<tr>")
response.Write("<td>" & rs("学号") & "</td>")
i = 0
end if 

''写课程,要找到合适的位置写,前面已经按课程排过序,所以不会错过
do while not rs("课程")=kcs(i)
i = i + 1
response.Write("<td> </td>")
loop 
response.Write("<td>" & rs("总分") & "</td>")
i = i + 1

rs.movenext
loop

endline

rs.close
conn.close

set rs=Nothing 
set conn=Nothing
%>
</table>
</body>
</html>
------解决方案--------------------
用这种方式试一下看看行不,代码简化了,请自己补充完整
<%
dim tmp,tmp1,tmp2,tmp3
tmp="<table>"
while not rs.eof
tmp1=tmp1 & "<td>"&rs("0")&"</td>"
tmp2=tmp2 & "<td>"&rs("1")&"</td>"
tmp3=tmp3 & "<td>"&rs("2")&"</td>"
rs.movenext
wend
tmp1="<tr>" & tmp1 & "</tr>"
tmp2="<tr>" & tmp2 & "</tr>"
tmp3="<tr>" & tmp3 & "</tr>"
tmp=tmp & tmp