Click to See Complete Forum and Search --> : code on paging


Squall Leonhart
11-24-2003, 01:09 PM
Hi, guys.
I have question on paging.
Please take a look at this code.


<HTML>
<HEAD>
</HEAD>
<BODY LEFTMARGIN=0 MARGINWIDTH="0" MARGINHEIGHT="0">
<%
'Dimension variables
Dim todaysDate
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database
Dim Page
Dim RowCount
Dim PageCounter

Page = Request.QueryString("Page")

'If there is no page set it to page 1
If Page = "" then
Page = 1
End if

RowCount = 0

Set adoCon = Server.CreateObject("ADODB.Connection")
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM tblMSI ORDER BY ID DESC"
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")
rsGuestbook.CursorType = 3
rsGuestbook.PageSize = 10
rsGuestbook.Open strSQL, adoCon
rsGuestbook.AbsolutePage = cInt(Page)
%>
<TABLE width="576" border="1" align="center" nowrap >
<br>
<tr>
<td colspan="7">
<DIV align="Center"><b>MSI.FYI Listings </b>- Current page <B><%=Request.Querystring("page")%></b></DIV>
</td>
</tr>
<% Do while not rsGuestbook.eof and RowCount < rsGuestbook.PageSize %>
<tr>
<td width="28">#<% Response.Write (rsGuestbook("ID")) %></td>
<td width="134"><% Response.Write FormatDateTime(rsGuestbook("Date"),2) %></td>
<td width="377"><a href="guestbook.asp?ID=<%=rsGuestbook("ID")%>" target="_blank"> MSI.FYI#<% Response.Write (rsGuestbook("ID")) %>&nbsp; <% Response.Write (rsGuestbook("title")) %></td>
</tr>
<% 'Write the HTML to display the current record in the recordset
rsGuestbook.Movenext
RowCount = RowCount + 1
Loop
%>
<tr>
<td bordercolor="#666666" align="Middle">
<%For PageCounter = 1 to rsGuestbook.PageCount %>
<A href='msi_fyi.asp?Page=<%=PageCounter %>' class='myclass'>Page&nbsp<%=PageCounter %></a>
&nbsp&nbsp&nbsp
<%Next%>
</td>
</tr>
</table>
<%'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
End if
%>
</BODY>
</HTML>


As you can see, this code generates link like page1, page2, page3, and so on.
If there is 100 pages total, it will go on like page1, page2.....page100. Then it's too much.

So how can I show page1, page2 ,page3, page4, page5-> and if I click ->, and I want to move to <-page6, page7,page8,page9,page10 ->.
And when I reach final page, I want to show it like this
<-page96, page97,page98,page99,page100.
Do you guys encounter this problem before?
Please help me. Thanks.

cmelnick
11-24-2003, 01:59 PM
You just need to do a conditional loop for the page number output...

Something like...


<%
Dim start
Dim finish

If Page < 11 Then
start = 1
finish = 11
ElseIf rsGuestbook.PageCount - Page < 11 Then
start = rsGuestbook.PageCount - 10
finish = rsGuestbook.PageCount
Else
start = Page - 5
finish = Page + 5
End If

For i = start To finish
If Not i = Int(Page) Then
%>
<a href="msi_fyi.asp?Page=<%=i%>"> Page <%=i%> </a>
<%
Else
Response.Write " Page " & i
End If
Next
%>



Haven't tested it, but it should work pretty well...you might have to tweak it a bit...

Squall Leonhart
11-24-2003, 02:49 PM
Thanks for reply.
But I don't know which part in my code need to be tweaked.
Could you show me more specifically? Thanks.

cmelnick
11-24-2003, 03:21 PM
the code I showed you should replace your lines:

<%For PageCounter = 1 to rsGuestbook.PageCount %>
<A href='msi_fyi.asp?Page=<%=PageCounter %>' class='myclass'>Page <%=PageCounter %></a>

<%Next%>

Squall Leonhart
11-24-2003, 03:39 PM
So I changed code like this

<HTML>
<HEAD>
</HEAD>
<BODY LEFTMARGIN=0 MARGINWIDTH="0" MARGINHEIGHT="0">
<%
'Dimension variables
Dim todaysDate
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database
Dim Page
Dim RowCount
Dim PageCounter

Page = Request.QueryString("Page")

'If there is no page set it to page 1
If Page = "" then
Page = 1
End if

RowCount = 0

Set adoCon = Server.CreateObject("ADODB.Connection")
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM tblMSI ORDER BY ID DESC"
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")
rsGuestbook.CursorType = 3
rsGuestbook.PageSize = 10
rsGuestbook.Open strSQL, adoCon
rsGuestbook.AbsolutePage = cInt(Page)
%>
<TABLE width="576" border="1" align="center" nowrap >
<br>
<tr>
<td colspan="7">
<DIV align="Center"><b>MSI.FYI Listings </b>- Current page <B><%=Request.Querystring("page")%></b></DIV>
</td>
</tr>
<% Do while not rsGuestbook.eof and RowCount < rsGuestbook.PageSize %>
<tr>
<td width="28">#<% Response.Write (rsGuestbook("ID")) %></td>
<td width="134"><% Response.Write FormatDateTime(rsGuestbook("Date"),2) %></td>
<td width="377"><a href="guestbook.asp?ID=<%=rsGuestbook("ID")%>" target="_blank"> MSI.FYI#<% Response.Write (rsGuestbook("ID")) %> <% Response.Write (rsGuestbook("title")) %></td>
</tr>
<% 'Write the HTML to display the current record in the recordset
rsGuestbook.Movenext
RowCount = RowCount + 1
Loop
%>
<tr>
<td bordercolor="#666666" align="Middle">
<%
Dim start
Dim finish

If Page < 11 Then
start = 1
finish = 11
ElseIf rsGuestbook.PageCount - Page < 11 Then
start = rsGuestbook.PageCount - 10
finish = rsGuestbook.PageCount
Else
start = Page - 5
finish = Page + 5
End If

For i = start To finish
If Not i = Int(Page) Then
%>
<a href="msi_fyi.asp?Page=<%=i%>"> Page <%=i%> </a>
<%
Else
Response.Write " Page " & i
End If
Next
%>
</td>
</tr>
</table>
<%'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
End if
%>
</BODY>
</HTML>

But still doesn't work. I don't know where is the problem.
I wanted to show it like
Page1,Page2, Page3,Page4,Page5 Next

Squall Leonhart
11-25-2003, 03:27 PM
Any ideas?:confused: