Tuesday, 29 November 2011

DISPLAY RECORDS HORIZONTALLY

Ok, when it comes to displaying records from a database on a web page, we can all agree that using a table to organize your data is one of the most commonly used methods. Today, most websites use tables to display their records vertically. However, depending on the type of data that you are displaying or the layout of your page, you may need to display your records horizontally. So how do you do this. Actually it's not that hard.

The first thing you need to do is create a database called MyDatabase. Then, create 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:



<% 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 ""
recCount = 0
Do UNTIL objRS.EOF

IF recCount Mod 3 = 0 THEN

IF recCount <> 0 THEN
Response.Write ""
Response.Write ""
ELSE
Response.Write ""
END IF

recCount = recCount + 1
objRS.MoveNext
Loop

Response.Write"
"&objRS("Item")&""&objRS("Item")&"
"
ELSE
Response.Write "Sorry, there are no records in our database."
END IF
%>

This script will return all of the records in your tblData table and display them in a table on your page. Plus, it will display three records horizontally in a table row and then 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.

Happy record displaying!

http://www.aspwebpro.com/aspscripts/database/displayhoriz.asp

No comments:

Post a Comment