動態|網頁
有時候,需要在某個表格列內放一定數量的文字和圖片,並需要隨著記錄數動態顯示一定的行數和列數,比如在一行內顯示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