Display Records Horizontally

When it comes to displaying records from a database on a web page, using a table to organize your data has long been one of the most commonly used methods.  Many websites use tables to display their records vertically.  However, you may also need to occasionally display your records horizontally.

To do this, create a database called MyDatabase and a table called tblData with these fields:
ID – autonumber
Item – text field

Next, create a page called /DisplayHoriz.asp and copy the below code into your page:

<!--#INCLUDE VIRTUAL="/includes/connection.asp" -->

<%
DIM mySQL, objRS
mySQL = "SELECT * FROM tblData"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn

DIM recCount
IF Not objRS.EOF THEN
Response.Write "<table width='100%'>"
recCount = 0
Do UNTIL objRS.EOF

IF recCount Mod 3 = 0 THEN

     IF recCount <> 0 THEN
     Response.Write "</tr>"
     Response.Write "<tr><td>"&objRS("Item")&"</td>"
     ELSE
     Response.Write "<td>"&objRS("Item")&"</td>"
     END IF

recCount = recCount + 1
objRS.MoveNext
Loop
Response.Write"</tr></table>"

ELSE
Response.Write "Sorry, there are no records in our database."
END IF
%>

This script will return all of your records and display them in a table on your page.  It will display three records horizontally in a table row, end the current row, start a new row, and display three more records.  If you want to display more records horizontally, simply change the “3” in this line: IF recCount Mod 3 = 0 THEN to however many records you want to display in each row.

< Back to ASP Scripts