...

View Full Version : not showing all records, making next and previous buttons



chris_angell
08-04-2004, 08:20 PM
hello i have made a forum and I am trying to make next and previous buttons to handle all the threads like

previous << 6 . 7 . 8 . 9 . 10 >> next


instread of

1 . 2 . 3 .4 . 5 . 6. 7 . 8 . 9 . 10 .11 . 12 .13 .14 . 15 .16 .17 etc

letting all the page numbers show


but I have never done this before and am trying to think of a way to do it.. at the moment my page counts the threads in 20s the makes a link to visist the next 20 and so on.. but i can work out how to break it up...

here is my code i am using, this breaks the threads into 20's

<%for t = 0 to fix((alleEintraege-1)/20)%>
<a href="index.asp?id=<%=id%>&s=<%=t%>">
<%if t=(s/20) then%>
<%end if%>
<%=t+1%><%if t=(s/20) then%>
<%end if%></a>
<%next%>

has anyone got any ideas how i could accomplish my goal.. ???? and get only 10 page numbers to show at a time ???
:confused:

Morgoth
08-04-2004, 10:25 PM
Indenting is your friend.

First off, you code can be a little bit more clean than that. And it looks like you want 20 records to be displyed per page, so it would be good to use a variable for that number. I don't know what alleEintraege means, but it looks like it's the number of all the records in the database/table for this page.

You're code is strange because I don't know where you get s from, unless it's suppose to be the page amount your on now, or something.

Let's see what we can do.

This is the method I thought off, I will post the code I come up with as soon as I pick out the bugs.

You will count how many pages you will have.

Example we will use:
Number of pages = 20
TotalNumberOfRecords = 100
RecordsPerPage= 5
NumberOfPages = TotalNumberOfRecords / RecordsPerPage
20 = 100 / 5

Find out what Page you are on, and make it the median. This will work if the page you are on is number 5 all the way up to 15, but we will need something else for numbers 1-4 and 16-20. We will also need to know how this will work with a low number of records, like 5,2,1 pages worth.

So we roughly have an idea to start, let see what I can come up with for the rest of the script.

Morgoth
08-04-2004, 11:06 PM
I wrote up this code, and also put it on a page I would need a page counter like this for.

All my variables are self explanatory.



<%
If Not IntPageNumber <= 1 Then
Response.Write "<a href=""YourPage.asp?page=" & IntPageNumber - 1 & """><<</a> "
End If

Do Until IntPageCount = IntNumberOfPages
IntPageCount = IntPageCount + 1
If IntPageCount <> IntPageNumber Then
If IntPageCount <= IntPageNumber + 3 And IntPageCount >= IntPageNumber - 3 Then
Response.Write "<a href=""YourPage.asp?page=" & IntPageCount & """>" & IntPageCount & "</a> "
End If
Else
Response.Write IntPageCount & " "
End If
Loop

If Not IntPageNumber >= IntNumberOfPages Then
Response.Write "<a href=""YourPage.asp?page=" & IntPageNumber + 1 & """>>></a>"
End If
%>

Change YourPage.asp to your page (3)
Change the numbers in bold into how many you want to be shown. (2)

The way it works is 100% simple. The code I added to mine to make this little script work is:


If IntPageCount <= IntPageNumber + 3 And IntPageCount >= IntPageNumber - 3 Then

End If


This will check and make sure only 3 other pages will be shown before the current page, and after the current page. If you want exactly ten pages shown, including the one you want then one of these numbers will be 4, and the other 5. Or just make them both 5 for 10 other pages shown, plus the current one.

If you need anything explained, just reply with your question.

chris_angell
08-05-2004, 12:18 AM
Morgoth.. I would just like to say that a response to a thread has never made me smile so much... I AM VERY IMPRESSED..

thank you.. and thanks for taking the time to help... maybe one day I could re-pay the favor ??? right..


take it easy and thanks again

Morgoth
08-05-2004, 03:44 AM
I just realized that there is a problem that you might not need, but I think this is what you wanted.

If I have 20 pages, and I am on page 10, you will see (with a radius of 5 other pages):
<< 5 6 7 8 9 10 11 12 13 14 15 >>

But if I were on page 1, you will see:
<< 1 2 3 4 5 6 >>

Or page 20:
<< 15 16 17 18 19 20 >>

Or near the edge:
<< 14 15 16 17 18 19 20 >>
<< 1 2 3 4 5 6 7 8 >>


This is what I want to do, and maybe this is what you want:


<< 10 11 12 13 14 15 16 17 18 19 20 >>
<< 10 11 12 13 14 15 16 17 18 19 20 >>
<< 10 11 12 13 14 15 16 17 18 19 20 >>
<< 8 9 10 11 12 13 14 15 16 17 18 >>



Can you see? Always keep the same amount. 10 pages to pick from and, of course, 1 current page.

I will make this code, and post it. If you want it you can take it, or not.

chris_angell
08-05-2004, 10:46 AM
hi,,, i have tried to combine the code you posted with my previous code. ??? but sadly it is not working and I have error messages coming out my ears.. evil server error 500.... if you could aid me a little more how to do this i would be ever so greatful...

here is more of the code I am using... i hope this helps a little more


sql = "select count(id) as alleEintraege from postings where connected = 0"
rst.open sql, connect, ado_Openkeyset, ado_LockOptimistic
alleEintraege = int(rst("alleEintraege"))
%>
<tr>
<td width="694" bgcolor="#3E6A60" colspan = "6" class = "MnWhTxt" height="1">
<p align="center"><img border="0" src="../images/forum_bottom_1.jpg"></td>
</tr>
<tr>
<td width="694" bgcolor="#3E6A60" colspan = "6" class = "MnWhTxt" height="14">
<p align="right" class="MnWhTxt">Page


<%for t = 0 to fix((alleEintraege-1)/20)%>
<a class="LkWhiteBDNew" href="index.asp?id=<%=id%>&s=<%=t%>"><%if t=(s/20) then%><b><%end if%><%=t+1%><%if t=(s/20) then%></b><%end if%></a>&nbsp;
<%next%>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%
rst.close
if not(id=0) then %>


i hope this will help a little more..

:D

Morgoth
08-05-2004, 08:01 PM
Please use the [ code ] tags to post your code. It makes it easier to read, and it keeps indents intacted.

Here's your code in code tags, proberly indented (imo).


</table>
</tr>
</td>
<table>
<%
sql = "select count(id) as alleEintraege from postings where connected = 0"
rst.open sql, connect, ado_Openkeyset, ado_LockOptimistic
alleEintraege = int(rst("alleEintraege"))
%>
<tr>
<td width="694" bgcolor="#3E6A60" colspan = "6" class = "MnWhTxt" height="1">
<p align="center"><img border="0" src="../images/forum_bottom_1.jpg">
</td>
</tr>
<tr>
<td width="694" bgcolor="#3E6A60" colspan = "6" class = "MnWhTxt" height="14">
<p align="right" class="MnWhTxt">Page
<%
For t = 0 To Fix((alleEintraege - 1) / 20)

Response.Write "<a class=""LkWhiteBDNew"" href=""index.asp?id=" & id & "&s=" & t & """>"

If t = (s / 20) Then
Response.Write "<b>"
End If

Response.Write t + 1

If t = (s / 20) Then
Response.Write "</b>"
End If

Response.Write "</a>&nbsp;"

Next
%>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%
rst.close
If Not (id = 0) Then
'Nothing?
End If
%>


Now here's your code with comments telling you where to put the code I posted. I have a problem though, because you're missing some variables that are required. I added some code to find out some of the variables, and I don't know what id is for. If it's for the id number of the records you are trying to view, it's in the wrong spot.



</table>
</tr>
</td>
<table>
<%
sql = "select count(id) as alleEintraege from postings where connected = 0"
rst.open sql, connect, ado_Openkeyset, ado_LockOptimistic
alleEintraege = int(rst("alleEintraege")) 'Total number of records
IntRecordsPerPage = 20 'Total number of records per page. This can easily be changed.

IntNumberOfPages = CInt((alleEintraege - 0.5) / IntRecordsPerPage + 0.5) 'Find out how many pages there will be.

If Request.QueryString("s") <> "" Then
IntPageNumber = Int(Request.QueryString("s"))
Else
IntPageNumber = 1
End If
%>
<tr>
<td width="694" bgcolor="#3E6A60" colspan = "6" class = "MnWhTxt" height="1">
<p align="center"><img border="0" src="../images/forum_bottom_1.jpg">
</td>
</tr>
<tr>
<td width="694" bgcolor="#3E6A60" colspan = "6" class = "MnWhTxt" height="14">
<p align="right" class="MnWhTxt"><b>Page:</b>
<%
If IntPageNumber > 1 Then
Response.Write "<a class=""LkWhiteBDNew"" href=""index.asp?id=" & id & "&s=" & IntPageNumber - 1 & """>Prev <<</a> "
Else
Response.Write "Prev << "
End If

For t = 1 To Fix(alleEintraege / IntRecordsPerPage)

Response.Write "<a class=""LkWhiteBDNew"" href=""index.asp?id=" & id & "&s=" & t & """>"

If t = IntPageNumber Then
Response.Write "<b>" & t & "</b>" 'Write the current pages with bold to stand out.
Else
If t <= IntPageNumber + 4 And t >= IntPageNumber - 4 Then 'Only show 4 other pages infront or behind current page.
Response.Write t 'Write the other pages without bold.
End If
End If

Response.Write "</a> "

Next

If IntPageNumber < IntNumberOfPages Then
Response.Write "<a class=""LkWhiteBDNew"" href=""index.asp?id=" & id & "&s=" & IntPageNumber + 1 & """>>> Next</a> "
Else
Response.Write ">> Next "
End If

%>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%
rst.close
If Not (id = 0) Then
'Nothing?
End If
%>


I have not gotten to the script where it keeps the same number of pages on the screen at a time, but I will get to it.

The code above is only a guide. It will work, but I suggest you read it, and ask me questions, because you will not learn anything unless you understand what the code is doing.

Morgoth
08-06-2004, 04:57 AM
Ok, here's the code for keeping a static amount of pages to choose from. If you have IntPagesAround set at 5, then there is will 10 pages to choose from, and 1 current page.

What ever is in Italics is the new code I added since my last post.



</table>
</tr>
</td>
<table>
<%
Dim IntLeftPages, IntRightPages, IntPagesAround, IntNumberOfPages, IntRecordsPerPage, IntPageNumber 'Declare variables

sql = "select count(id) as alleEintraege from postings where connected = 0"
rst.open sql, connect, ado_Openkeyset, ado_LockOptimistic
alleEintraege = int(rst("alleEintraege")) 'Total number of records
IntRecordsPerPage = 5 'Total number of records per page. This can easily be changed.

If Request.QueryString("s") <> "" Then
IntPageNumber = Int(Request.QueryString("s"))
Else
IntPageNumber = 1
End If

IntNumberOfPages = CInt((alleEintraege - 0.5) / IntRecordsPerPage + 0.5) 'Find out how many pages there will be.
IntPagesAround = 5 'Only show 5 other pages infront or behind current page.
IntRightPages = IntPagesAround
IntLeftPages = IntPagesAround

If IntPageNumber <= IntPagesAround Then
IntRightPages = IntPagesAround * 2 - IntPageNumber + 1
End If

If IntPageNumber >= IntNumberOfPages - IntPagesAround Then
IntLeftPages = IntPagesAround * 2 - IntNumberOfPages + IntPageNumber
End If
%>
<tr>
<td width="694" bgcolor="#3E6A60" colspan = "6" class = "MnWhTxt" height="1">
<p align="center"><img border="0" src="../images/forum_bottom_1.jpg">
</td>
</tr>
<tr>
<td width="694" bgcolor="#3E6A60" colspan = "6" class = "MnWhTxt" height="14">
<p align="right" class="MnWhTxt"><b>Page:</b>
<%
If IntPageNumber > 1 Then
Response.Write "<a class=""LkWhiteBDNew"" href=""index.asp?id=" & id & "&s=" & IntPageNumber - 1 & """>Prev <<</a> "
Else
Response.Write "Prev << "
End If

For t = 1 To Fix(alleEintraege / IntRecordsPerPage)

Response.Write "<a class=""LkWhiteBDNew"" href=""index.asp?id=" & id & "&s=" & t & """>"

If t = IntPageNumber Then
Response.Write "<b>" & t & "</b>" 'Write the current pages with bold to stand out.
Else
If t <= IntPageNumber + IntRightPages And t >= IntPageNumber - IntLeftPages Then
Response.Write t 'Write the other pages without bold.
End If
End If

Response.Write "</a> "

Next

If IntPageNumber < IntNumberOfPages Then
Response.Write "<a class=""LkWhiteBDNew"" href=""index.asp?id=" & id & "&s=" & IntPageNumber + 1 & """>>> Next</a> "
Else
Response.Write ">> Next "
End If

%>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
<%
rst.close
If Not (id = 0) Then
'Nothing?
End If
%>

chris_angell
08-08-2004, 02:52 PM
thanks Morgoth I have got it working at it is looking good and doing its job :)

I couldn't have done it without your help... after looking at the code and breaking it down.. i see how it was all possible...

if you want to see your hard work in its full glory go and have a look at the forum.. here is the link

http://www.theordinaryboys.com

many thanks again... take it easy

chris



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum