日期:2010-11-09  浏览次数:20991 次

有时候,需要在某个表格列内放一定数量的文字和图片,并需要随着记录数动态的显示一定的行数和列数,比如在一行内显示4张图片抑或是5张图片,用Table实现的时候有时候很难判断tr或者td的结束位置,这里我写了一段代码,能实时判断行列的结束位置,记录集中的数据不足时能自动添加空tr和td。

测试用表格

'Create Table Test(id int,sTitle varchar(50))

程序代码

'测试每行排4列,排四行
dim i,j
dim iRsCount,iRows
dim perLines
perLines=4  '每行需显示的列数
set objRs=server.CreateObject("adodb.recordset")
objRs.open "select top 16 * from Test order by id",conn,3,1
if not objRs.eof then
   response.write "<table width=""100%"" cellpadding=""0"" cellspacing=""8"">"&vbcrlf
   iRsCount=objRs.recordcount  '取得实际的记录数
    iRows=int(iRsCount/perLines) '计算可得到的行数
    if iRows<1 then
       iRows=1
     else      
        if iRsCount mod perLines=0 then
          iRows=int(iRsCount/perLines)
         else
          iRows=int(iRsCount/perLines)+1
        end if
    end if
   i=1
   while not objRs.eof
     for i=1 to iRows
      response.write "<tr>"&vbcrlf
      for j=1 to perLines
        If Not objRs.eof then
           response.write "<td width=""25%""><div align=""center"">"&objRs("sTitle")&"</div></td>"&vbcrlf
        Else
           response.write "<td> </td>"&vbcrlf
        End if
        if not objRs.eof then objRs.movenext
      next
      response.write "</tr>"&vbcrlf
    next
  Wend
  response.write "</table>"
end if
objRs.close
set objRs=nothing