Database Paging

Database paging essentially means extracting data from your database and displaying in a neat eye-pleasing format.

With this script we, establish a connection to a database using a standard connection string of JET drivers.

Use the following script exactly in the same format and order as it has been given.

<%
Dim show, i, j, k, RS, numb, no, DB, num
Set num=10
Set DB = Server.CreateObject("ADODB.Connection")
DB.Mode = adModeReadWrite
DB.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("database.mdb") + ";Persist Security Info=False")
Set show=DB.execute("SELECT * FROM table ORDER BY ID field")
if not (show.bof and show.eof) then
show.movefirst
If (Request("page_num"))<>"" then
i=Request("page_num")
Else
i=1
End if

for k=1 to ((i-1)*num)
show.movenext
next
while not show.eof and j<=num-1 %>

'Items to display.. A sample is given below

<TABLE>
<TR>
<TD>
Response.write(show("field"))
</TD>
</TR>
</TABLE>

<!-- Navigation Control for Sample -->

<table align="center" width="80%" border="0" cellpadding="5">
<tr>
<td align="center"> <%
Set RS= DB.execute("SELECT * FROM table")
k=1
If not(RS.bof and RS.eof) then
RS.movefirst
While not (rs.eof)
RS.movenext
k=k+1
Wend
End if
k=k-1
If (k mod num)<>0 then
numb = Int(k/num)+1
else
numb = Int(k/num)
End If
no=1
If ((i-1)<>0) then Response.Write("<a href=page.asp?page_num=" & (i-1) & ">Previous</a>&nbsp;&nbsp;")
for no=1 to numb
If (Cstr(no)=Request("page_num")) then
Response.Write("<b>" & no & "</b>" & "&nbsp;&nbsp;")
Else If Request("page_num")="" then
If (no<>1) then
Response.Write("<a href=page.asp?page_num=" & no & ">" & no & "</a>&nbsp;&nbsp;")
else
Response.Write("<b>" & no & "</b>" & "&nbsp;&nbsp;")
end if
else
Response.Write("<a href=page.asp?page_num=" & no & ">" & no & "</a>&nbsp;&nbsp;")
End If
End If
next
If (Cint(Request("page_num"))<numb) and (numb>1) then Response.Write("<a href=page.asp?page_num=" & (i+1) & ">Next</a>")
Else %>
<tr>
<td colspan="3">There are no entries</td>
</tr>
<% End If%>
</table>


The above script has to be used as it is. Only change the variable in the second line which decides the number of entries required per page.

Also change the path and name of your database, which you shall find in the first few lines which contain the connection string.

In case you are stuck somewhere, you can always contact me using the contact details.

 
Site best viewed in IE 5.5+ with 800 X 600 resolution
© Drumster, 2003