Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Paging: Displaying page links where there are hundreds of them 1

Status
Not open for further replies.

meumax

Programmer
Apr 13, 2002
38
AU
I need to display a recordset in a paged manner but sometimes there are so many pages that it becomes cumbersome to display all the page links.

I wrote some crude code to display my page links like this:

1 2 3 4 5 [6] 7 8 9 10

Once you get past 6 it goes like this:

2 3 4 5 6 [7] 8 9 10 11

3 4 5 6 7 [8] 9 10 11 12 and so on.

My code uses a bunch of ugly If statements to achieve the result, but I want to know if there is a more elegant solution available.

There must be some standard algorithm that people use to achieve this result as I see it implemented on a lot of web sites on the Internet. I want to know what it is.
 
Its fairly simple... First you need to figure out how many results will go on one page.. for this example, we'll use 5... then you need to get all the results from the database when the user selects which page they want to view..

now you have everything you need to accomplish your paging method..

take the page number.. lets say page 5.. and multiply that minus 1 by the number of items per page (5).. so thats 4 * 5 = 20..

Record 20 is your starting record.. and obviously 25 will be your last displayed record.. so now you display it..

<%
set db = server.createobject (&quot;adodb.connection&quot;)
set rs = server.createobject (&quot;adodb.recordset&quot;)

db.open &quot;DSN=DSNName;UID=username;PWD=password&quot;
rs.open &quot;SELECT * FROM Table&quot;,db

' Get start of records for page
RecordStart = (page - 1) * ItemsPerPage

' Move to specified starting point
rs.MoveTo = RecordStart 'which in this case is 20

'Show the number of items per page or until EOF
items = 0
do while rs.eof <> false
items = items + 1
if items > 5 then
exit loop
end if
... your recordset items ...
rs.movenext
loop

' This section figures out how many pages there are
TotalItems = rs.count
pages = TotalItems / ItemsPerPage

' If pages has decimal places, then there is a page with less than 5 items, and we still need to show that as a page
if int(pages) < pages then
pages = int(pages) + 1
else
pages = int(pages)
end if

'write out the pages
response.write &quot;PAGE: &quot;
for i = 1 to pages
response.write &quot;[&quot; & i & &quot;] &quot;
next


' close down all connections
rs.close
db.close
set rs = nothing
set db = nothing
%>


Hope this helps,

Gorkem.
 
I don't have a problem knowing which records to display for which page. My problem is in displaying my page navigation links at the bottom of my table. I want previous and next buttons but ALSO direct page links BUT only 10 at a time.

i.e. <prev 23 24 25 26 27 [28] 29 30 31 32 next>
<prev 14 15 16 17 18 [19] 20 21 22 23 next?
<prev 1 2 3 4 5 [6] 7 8 9 10 next>

Sometimes I have so many records that there can be up to 57 pages worth of records. Now it would look a bit silly to display 57 individual page navigation links at the bottom of my table e.g. <prev 1 2 3 4 ..... 55 56 57 next>

Basically I want a replacement for this part:

for i = 1 to pages
response.write(&quot;<a href='results.asp?page=&quot; & i & &quot;'>&quot; & i & &quot;</a>&quot;)
Next

So that it only displays 10 page navigation links at a time if there are more than 10 pages. Have a look at popular search engines such as google.com to see what I mean.
 
Hmm, this is on the fly, but here's my shot at it :)
Code:
'Pretend we have two var's already called curPage and maxPage
'curPage has the value for current page
'maxPage has the value for maximum number of pages

Dim st, en, pflag, nflag

If curPage > 5
	st = curPage - 5
	pflag = true
Else
	st = 1
	pflag = false
End If

If maxPage - st > 10 Then
	en = st + 9
	nflag = true
Else
	nflag = false
	en = maxPage
End If

'If there are previous records, show prev link
If pFlag = true Then 
	Response.Write &quot;<a href='results.asp?page=&quot; & curNum-1 & &quot;'>&lt;prev</a>&quot;
Else
	Response.Write &quot;&lt;prev&quot;
End If

Dim i
For i = st to en
	If i = curNum Then
		Response.Write &quot;[&quot; & i & &quot;]&quot;
	Else
		Response.Write &quot;<a href='results.asp?page=&quot; & curNum-1 & &quot;'>&quot; & i & &quot;</a>&quot;
	End If
Next

'If there are next records show the next link
If nFlag = true Then 
	Response.Write &quot;<a href='results.asp?page=&quot; & curNum-1 & &quot;'>&lt;prev</a>&quot;
Else
	Response.Write &quot;next&gt;&quot;
End If

-Tarwn ------------ My Little Dictionary ---------
Extreme Programming - (1)Trying to code before my second cup of coffee. (2) While(1){ Ctrl+C; Ctrl+V; }
FAQ - Web-ese for &quot;Forget Asking Questions, I am to busy&quot; :p
 
meumax,

Did you manage to get this sorted?

My reason for asking is this is something that I really need as well and I cant work out a tidy solution either. - FateFirst
 
Tarwn,

Just changed a few things with your code instead, works great!

Thanks a lot, theres a star heading your way :p - FateFirst
 
Thanks, glad to be of help :)
Out of curiosity, what needed changing to make it work? i wrote it on th fly so didn't get the opportunity to test it out, may want to save a copy for future reference
-Tarwn ------------ My Little Dictionary ---------
Extreme Programming - (1)Trying to code before my second cup of coffee. (2) While(1){ Ctrl+C; Ctrl+V; }
FAQ - Web-ese for &quot;Forget Asking Questions, I am to busy&quot; :p
 
Tarwn,

I didnt change anything as in *it wasnt working*, just changed it slightly to work for the code I use to setup the paging...so all in all, your *fly code* :p works fine! - FateFirst
 
Ah, cool. Guess I should go ahead and copy it then before I fins myself 3 months down the road needing the same thing and writing it (yet) again from scratch, thanks much
-Tarwn ------------ My Little Dictionary ---------
Extreme Programming - (1)Trying to code before my second cup of coffee. (2) While(1){ Ctrl+C; Ctrl+V; }
FAQ - Web-ese for &quot;Forget Asking Questions, I am to busy&quot; :p
 
Lol...I've been there!

Still...keeps the brain working :p - FateFirst
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top