注册 登录
编程论坛 ASP技术论坛

三行四列的输出,有个小问题想不通!

awke002 发布于 2008-08-23 16:19, 1362 次点击
我是想在页面上输出一个三行四列的表格,格里面的数据室从数据库中绑定的。
可是为什么输出的都是同一个数据呢。
我用 rs.open "select top 12 * from students  order by id desc",conn,1,1
记录出了最后12位学生的学号。
下面是代码:
本意是输出这样的:
1      2     3     4
name1 name2 name3 name4
。。。。。。。。。。
9      10     11     12
name9 name10 name11 name12


可用下面的代码虽然实现了三行四列,但是输出却是这样的:

1      1   1    1
name1 name1 name1 name1
。。。。。。。。。。
1      1    1   1
name1 name1 name1 name1


为什么会这样;找了一天了。。

<table width="100" border="0" cellspacing="0" cellpadding="0">
  <tr>
  
    <%
  for i = 1 to 12
  %>
    <td>
    <table width="100" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="50" align="center"><img src="/photo/<%=rs("stud_url")%>" /></td>
      </tr>
      <tr>
        <td align="center"><%=rs("stud_name")%></td>
      </tr>
    </table>
    </td>
    <%
    if i mod 4 = 0 then
      response.Write "<tr></tr>"
    end if
    next
    %>
    
  </tr>
</table>
6 回复
#2
hmhz2008-08-23 18:55
呵呵,你这样循环,输出的肯定是重复数据
程序代码:

<table width="100" border="0" cellspacing="0" cellpadding="0">
<tr>
<%
    Set rs= Server.CreateObject("ADODB.Recordset")
    rs.open "select top 12 * from students  order by id desc",conn,1,1
    i=1
    do while not rs.eof
%>
<td>
    <table width="100" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="50" align="center"><img src="/photo/<%=rs("stud_url")%>" /></td>
      </tr>
      <tr>
        <td align="center"><%=rs("stud_name")%></td>
      </tr>
    </table>
</td>
<%
if (i mod 4)=0 then response.write "</tr>"  '4列显示 mod 4
i=i+1
rs.movenext
loop
%>
</tr>
<%
rs.close
set rs=nothing
%>
</table>
#3
hahatuzi20002008-08-24 12:36
<table>
  <tr>
<%
for i=1 to 12
response.Write("<td>")
response.Write(i & "<br>")
response.Write("1234")
response.Write("</td>")
if i mod 4 =0 then
response.Write("</tr><tr>")
end if
next
%>
</tr></table>  
把1234换成你想要的数据库的数据就OK 了,自己改动一下吧.
用1234测试没有问题,不知道换成数据库的内容有没有问题,先试试吧.


此段代码还没有考虑STUD_URL呢.自己再改改看.

[[it] 本帖最后由 hahatuzi2000 于 2008-8-24 12:38 编辑 [/it]]
#4
sheic2008-08-24 14:27
以上方法都是表格的经典方法

如果实在搞不来 就用<li>+CSS的方法 直接循环也可以的
#5
flynet2008-08-24 20:33
3#的 不错
#6
peswe2008-08-24 22:09
记录指针都没移动,数据当然是一样的!~
用while就可以了!~
2楼正解!~
#7
awke0022008-08-28 14:16
谢谢 各位了哈!
问题在你们的帮助下解决了!!
1