Reply
find next record and previous record
Old 12-27-2006, 05:33 AM find next record and previous record
Junior Talker

Posts: 1
hi

i have 100 students details in a database with fields rollno, name, and marklist

i have a an asp page that list all those records

when i click on one of the record, i get the detail page that show all the
details
of that particular students. so i fetch only one record in that page. i mean i
have
only one record in the recordset.

now i would like to have a two links "next record" and "previous record" in that
detail page.
<a href="details.asp?rollno=<%=recset("rollno")%>">
as i have only one reocord in the recordset i dont know which one is next or
previos.
at present how i do is, i fetch all the records and inside a loop
i check the current student's rollno in the recordset. when i find i do
moveprevious to get the previous students rollno and i do movenext twice
to get the next students roll no. is there any other alternative for this
i mean i dont want to fetch all the records to find what is next and what is
previous.

thanking you
neon20 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 12-27-2006, 08:40 AM Re: find next record and previous record
chrishirst's Avatar
Super Moderator

Posts: 13,609
Location: Blackpool. UK
Assuming roll numbers may not be consecutive in the database.
When you submit with your re-query include the current rollno and a flag to mark prev or next in the querystring
such as
Code:
with response 
     .write "<a href="
     .write chr(34)
     .write "/pagename.asp?roll="
     .write RollNo
     .write "&dir=prev"
     .write chr(34)
     .write ">Prev</a>"
     .write " | "
     .write "<a href="
     .write chr(34)
     .write "/pagename.asp?roll="
     .write RollNo
     .write "&dir=next"
     .write chr(34)
     .write ">Next</a>"
end with
BTW the chr(34)s are so that a correct quote mark (") is in the link rather the more common single quote.

then your DB calls would be

Code:
dir = lcase(request.querystring("dir"))
rollno = cint(request.querystring("roll"))
dim i
' conn code and declarations left out 
objRS.CursorLocation = adUseClient
' need the above line so that objRS.recordcount doesn't return as -1
strSQL = "SELECT rollno_col as rollno FROM table WHERE criteria ;"
objRS.Open strSQL, objConn, adOpenStatic, adLockReadOnly, adCmdText

for i = 0 to objRS.recordcount -1 
     if rollno <> cint(objRS.fields("rollno") then
           objRS.movenext
     else
           if dir = "prev" then
                objRS.moveprevious
                exit for
           elseif dir = "next" then
                objRS.movenext
                exit for
           else
                exit for
           end if
next
                rollno = objRS.fields("rollno")
 ' get the current rollno record
                objRS.close

strSQL = "SELECT fieldlist FROM table WHERE rollno_col = rollno ;"
objRS.Open strSQL, objConn, adOpenStatic, adLockReadOnly, adCmdText
objRS now contains the next or previous record
' process the rest of the page
the above code NOT tested just typed in
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System

Last edited by chrishirst : 12-27-2006 at 08:44 AM. Reason: hit submit instead of preview
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-04-2007, 06:22 PM Re: find next record and previous record
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,024
Name: Forrest Croce
Location: Seattle, WA
chrishirst gave you some great advice, but for extra credit, let's change the SQL a little bit, so that your page only asks for the one particular record it's going to use, instead of all of them, managing the "current" pointer through a cursor. As you add more and more students to the table, you'll want to put less pressure on the system.

You'll pass the current record ID and the direction ( next/prev ) in the query string. Based on that, you'll use:

Select Top 1 * From MyTable Where rollno < ID or
Select Top 1 * From MyTable Where rollno > ID

That works for Microsoft ( Access/SQL Server ); with Oracle you'd add And RowNum = 1 to the where clause instead of Top 1. I think in MySql it's Limit 1 at the end of the query, but I'm not sure.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Reply     « Reply to find next record and previous record
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13713 seconds with 12 queries