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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Paging offset by ID

Status
Not open for further replies.

ro6er

Programmer
Jul 1, 2003
76
0
0
Is it possible to page through a db offset by ID. For example. A user picks id 56 which is between 30 and 101 and then can go back and forth between these numbers. I can't seem to get my head around it. Here's the code I have been using and not yet changed to include this. Thanks for any help. Roger

Code:
<%
' records per page
recordsonpage = 1

' count all records
allrecords = 0
set rs = conn.Execute("SELECT * FROM photos")
do until rs.EOF
  allrecords = allrecords + 1
  rs.movenext
loop

' if offset is zero then the first page will be loaded
offset = request.querystring("offset")
if offset = 0 OR offset = "" then
  requestrecords = 0
else
  requestrecords = requestrecords + offset
end if

' opens database
session("fullname") = request("fullname")

'set rs = conn.Execute("SELECT * FROM Photos where ID = "&session("ident")&"")
set rs = conn.Execute("SELECT * FROM Photos where fullname = '"&session("fullname")&"'")

' reads first records 
hiddenrecords = requestrecords
do until hiddenrecords = 0 OR rs.EOF
  hiddenrecords = hiddenrecords - 1
  rs.movenext
  if rs.EOF then
    lastrecord = 1
  end if
loop
%>


<table cellspacing="2" cellpadding="2" border="1" width="400">
  <tr>
    <td><% if requestrecords <> 0 then %>
      <a href="?offset=<% = requestrecords - recordsonpage %>&fullname=<%=session("fullname")%>">Prev Page</a>
      <% else %>
      Prev Page
      <% end if %></td>
    <td><% if lastrecord <> 1 then %>
      <a href="?offset=<% = requestrecords + recordsonpage %>&fullname=<%=session("fullname")%>">Next Page</a>
      <% else %>
      Next Page
      <% end if %></td>
  </tr>
  <tr></tr>
</table>

<table cellspacing="2" cellpadding="2" border="1" width="400">

<%
' prints records in the table
showrecords = recordsonpage
recordcounter = requestrecords
do until showrecords = 0 OR rs.EOF
recordcounter = recordcounter + 1
%>

 <tr>
  <td><b><% = recordcounter %>
    recordcounter</b></td>
  <td><% = rs("PhotoName") %></td>
  <td><% = rs("ID") %>
    ID</td>
 </tr>

<%
  showrecords = showrecords - 1
  rs.movenext
  if rs.EOF then
    lastrecord = 1
  end if
loop
%>

</table>

<p>

<%
' Closes connection
rs.close
Conn.close
%>
 
A user picks id 56 which is between 30 and 101 "

My links would be look like this:

Code:
<a href=?id=56&p=p>Previous page</a>
<a href=?id=56&p=n>Next page</a>


The page would start with a check on querystring("id"). If none is given: then the defaultpage.

Then the querystring("p") is used in the SQL:


Code:
if request.querystring("p") = "p" then

 cSQL = "SELECT TOP 1 * FROM Photos where ID < " & _
   request.querystring("id")

else
 cSQL = "SELECT TOP 1 * FROM Photos where ID > " & _
   request.querystring("id")


end if


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top