預設 Recordset 迴圈輸出為直向列表,若需像商品目錄般「每列 4 欄」橫向顯示,可利用模數運算控制 <tr> 與 <td> 標籤輸出時機。
<%
sql = "SELECT * FROM serr ORDER BY id ASC"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 1, 1
%>
<style>
body, td, th { font-size: 12px; }
</style>
<table width="100%" align="center">
<%
For i = 1 To rs.RecordCount
' 每 4 筆開啟新列
If (i Mod 4 = 1) Then Response.Write ""
Response.Write "" & rs("title") & "
" & _
rs("con") & "
" & rs("www") & "
" & rs("keyword") & " "
' 每 4 筆關閉列
If (i Mod 4 = 0) Then Response.Write " "
rs.MoveNext
Next
' 若最後一列不滿 4 欄,補上閉合標籤
If (rs.RecordCount Mod 4 <> 0) Then Response.Write String(4 - (rs.RecordCount Mod 4), " ") & ""
rs.Close
%>
</table>
For i = 1 To rs.RecordCount 而非 Do While Not rs.EOF,方便取得索引 i。i Mod 4 = 1 時寫入 <tr> 開啟新列。i Mod 4 = 0 時寫入 </tr> 關閉列。<td> 並關閉列,確保 HTML 結構完整。cols,即可動態調整欄數。