Tuesday, 29 November 2011

DISPLAY 10 RECORDS PER PAGE

There may be times when you need to display a large number of records on your web page. Generally, the best way to do this is to limit the number of records displayed on your page and let the user scroll through the records by page. For example, let's say that you had a customer database that you wanted to display on your web page for your employees to view.

The first thing you need to do is create a database called MyCustomers. Then create a table called tblCustomerInfo with these fields:
ID - autonumber
Email - text field
Name - text field

Then, enter some test data. For this example, we will say that we have a database containing 30 customers and we only want to display 10 customers per page.

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

<% DIM mySQL, objRS mySQL = "SELECT * FROM tblCustomerInfo" Set objRS = Server.CreateObject("ADODB.Recordset") objRS.CursorType = 1 objRS.Open mySQL, objConn DIM intPageRecords, intRecords, intRecordCount, intCurrentPage DIM intNumberOfPages, intDisplayPage intPageRecords = Request.Querystring("page") IF intPageRecords = "" THEN intPageRecords = 1 : intRecords = 1 intRecords = intPageRecords intPageRecords = ((intPageRecords - 1) * 10) +1 intRecordCount = 0 IF NOT objRS.EOF THEN objRS.Move (intPageRecords - 1) DO WHILE intRecordCount < 10 and NOT objRS.EOF %> <% objRS.MoveNext intRecordCount = intRecordCount +1 Loop END IF %>
<%=objRS("Email")%> - <%=objRS("Name")%>

<%=intPageRecords%> - <%=intPageRecords+(intRecordCount-1)%> of <%=(objRS.RecordCount)%> customers

Scroll Through More Customers
<% intCurrentPage = Request.Querystring("page") IF intCurrentPage = "" THEN intCurrentPage = 1 intNumberOfPages = int(objRS.RecordCount \ 10) IF objRS.RecordCount MOD 10<> 0 THEN intNumberOfPages = intNumberOfPages + 1
Response.Write("Pages: [")
FOR intDisplayPage = 1 TO intNumberOfPages
IF Cint(intDisplayPage) = Cint(intCurrentPage) THEN
Response.Write " " & intDisplayPage & " "
ELSE
Response.Write " " & intDisplayPage &_
"
"
END IF
NEXT
Response.Write ("]")
%>

Finished! You now have your very own scrollable customer list. Now all you have to do is go out and get those customers!

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

No comments:

Post a Comment