Tuesday, 29 November 2011

DISPLAY TOP 10 RECORDS

Sometimes when you display records from a database on the web, you only want to display the most recent records. The Our Latest Scripts list on our home page only displays the top 10 records ordered by date. Here is how you do it.

The first thing you need to do is create a database called MyDatabase. Then create a table called tblScripts with these fields:
ID - autonumber
Script - text field
DateEntered - date/time field *** Also add "=Date()" as the default value for the field. This will add the date automatically, everytime a new record is entered. ***

Then, start adding some data to your database. If you leave the database empty, the script will simply display a blank page.

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



<% DIM mySQL, objRS mySQL = "SELECT TOP 10 * FROM tblScripts ORDER BY DateEntered DESC" Set objRS = Server.CreateObject("ADODB.Recordset") objRS.Open mySQL, objConn %>

<% DIM iRecordCount iRecordCount = 0 DO WHILE NOT objRS.EOFand iRecordCount <> 10 %>

Our Latest Scripts
<%=objRS("Script")%>


<% iRecordCount = iRecordCount + 1 objRS.MoveNext Loop objRS.Close Set objRS = Nothing objConn.Close Set objConn = Nothing %>

This will display the top 10 most recent records that were entered into your database

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

No comments:

Post a Comment