<%
dim s
s=Request.Form("txtserach")
' If the form was submitted to tell us how many results per page to use,
' redirect the user to the page and use give it the correct querystring
' information for the number of rpp
If Request.Form("rpp") <> "" Then
Response.Redirect("search.asp?rpp=" & Request.Form("rpp"))
' Response.Redirect("trial.asp?s=" & Request.Form("txtserach"))
End If
' Connect to database using a DSN-less connection
cString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("search.mdb")
' Connect to database using a DSN connection; uncomment to use
'cString = "DSN=NAME_OF_DSN"
' If the querystring rpp (results per page) is a valid number, use it
If Len(Request.QueryString("rpp")) > 0 AND IsNumeric(Request.QueryString("rpp")) Then
rpp = Request.QueryString("rpp") - 1 + 1
' Otherwise, display all records (you'll see why I used 0 in a minute)
Else
rpp = 0
End If
' If the starting point is a valid number, use it
If Len(Request.QueryString("start")) > 0 AND IsNumeric(Request.QueryString("start")) Then
start = Request.QueryString("start") - 1 + 1
' Otherwise, start with the first record
Else
start = 0
End If
' Create a RecordSet
Set RS = Server.CreateObject("ADODB.RecordSet")
' Find the total number of records from tbl_users,
' and save that value as a field called numCount,
' so we can access it in a second
Sql = "SELECT COUNT(*) as numCount FROM result"
' Open up the RecordSet
RS.Open Sql, cString
' Use the number of records that we just saved in Access
numRecords = RS("numCount")
' Close it up
RS.Close
' Good bye!
Set RS = Nothing
' If rpp is 0, it means we want to display all records.
' Now that we have the value for the total number of records,
' we can actually apply it here.
If rpp = 0 Then
rpp = numRecords
End If
' If the user typed in a querystring start value that is greater
' than the actual number of records, we've got a problem
' (numrecords - 1, because start always begins at 0, not 1)
If start > numRecords - 1 Then
' Make starting point 0 (the first record)
start = 0
' Loop through and find the last page number
While start + rpp < numRecords
start = start + rpp
WEnd
End If
' Sub to write out the Next and Previous buttons,
' so we don't have to reuse the same code at the
' top and bottom of the page
Sub writePageJump()
' If we're not starting at the first record, that means
' that there ARE previous records, and we need to display
' the button that allows the user to access them (boolean value)
prevButton = (start > 1 AND rpp < numRecords)
' If the user is not on the last page, then there's
' more records to be seen (boolean value)
nextButton = (start + rpp < numRecords)
%>
<% If prevButton Then %>
< Previous Page
<% Else %>
< Previous Page
<% End If %>
-
<% If nextButton Then %>
Next Page >
<% Else %>
Next Page >
<% End If %>
<%
End Sub
%>
<%
' Create a RecordSet
Set RS = Server.CreateObject("ADODB.RecordSet")
' Select all records from tbl_users ordered by ID
Sql = "SELECT * FROM result where keywords = '"+s+"'"
' We need to be able to move to a particular starting point,
' and we can only do that if we have the correct permissions
' (we need the right Cursor Type to allow us to move around)
RS.CursorType = 2
' Open this bad boy up
RS.Open Sql, cString
' If there are records matching our request, display them;
' otherwise we'll get an error
If NOT RS.EOF Then
' Move to the starting point
RS.Move(start)
' We want to know the number of records displayed, start at 0
numDisplayed = 0
' While we're not at the end of the recordset,
' and the number of records isn't above the number
' of results per page, keep displaying records
While NOT RS.EOF AND numDisplayed <= rpp - 1
%>
| ><%=RS("title")%> |
| <%=RS("discription")%> |
| <%=RS("page")%> |
<%
' Move on, and increase the displayed record count
RS.MoveNext()
numDisplayed = numDisplayed + 1
WEnd
End If
' Close 'er up
RS.Close
' Break 'er down
Set RS = Nothing
%>
<%=numDisplayed%> Result(s) Found
<%
' This part's a bit tricky... We want to display the current page number,
' as well as the total number of pages. So, we set 3 counters
i = 0
curPage = 0
numPages = 1
' Take the rounded number of pages (If there's 10 records,
' and we're displaying 5 per page, the total number of pages is 2)
numPages = CInt(numRecords / rpp)
' But... Since we ROUNDED, the we could be a page under the actual number of pages
If numPages < (numRecords / rpp) Then
numPages = numPages + 1
End If
' Loop through and find the current page
While i <= start
i = i + rpp
curPage = curPage + 1
WEnd
%>
|