ASP實現動態產生網頁中表格的行和列

來源:互聯網
上載者:User
動態|網頁

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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.