|
Reviewing the code :
<!--#include file="editme.asp"-->
<!--#include file="adovbs.inc"-->
Includes two files into the page. You are already aware of 'editme.asp' and 'adovbs.inc'
contains ADO constants.
<html>
<head>
<style>
body { font-family : Verdana; font-size : 8pt; }
</style>
</head>
<body>
Simple HTML head and body tags.
<%
On Error Resume Next
We discussed this statement earlier.
Dim geturl, title, description, keywords, strURL, strDB, rs, strKeyword
Dim urlshown, records, recordsToShow, n, con, currentPage, show
We declare a list of variables which we will be using later in our code.
If Len(Request.QueryString("currentPage")) = 0 Then
currentPage = 1
Else
currentPage = Request.QueryString("currentPage")
End If
Looks for currentPage variable in the query string. If it doesn't find
one ( which means that it is the first page ) then it sets the currentPage
variable to 1. Note currentPage variable is used when moving across pages
of records.
records = ""
recordsToShow = 10
n = 0
We declare some variables here. Important is recordsToShow. You
should change it to the number of records you want to show in one page. By default it is set to 10. Which
means that if more than 10 matching records are found in the Search Engine database, then it will display
10 of them in a page and will give a 'Next' link to the next ten or so records.
' Keyword to search
strKeyword = split(Trim(Request.QueryString("look_for")), " ")
We populate the strKeyword array with the keyword/s entered on the Form on
'search.htm' page. So strKeyword array contains the list of keywords to search in the
database.
' Our Connection Object
Set con = CreateObject("ADODB.Connection")
con.Open strDB
Create the Connection object and open the database for search.
' Our Recordset Object
Set rs = CreateObject("ADODB.Recordset")
rs.CursorLocation = adUseClient
rs.CacheSize = recordsToShow
show = True
Create the Recordset object and set the CursorLocation to
adUseClient. Why do we do this ? well in order to keep track of the records we have shown to
the user and then to show next records to the user on the next page and don't repeat the same
records again.
' Searching the records for the keywords entered
Select Case UBound(strKeyword)
Case 0 rs.Open "select * from all_pages where keywords like '%" _
& strKeyword(0) & "%' order by mydate desc", con
Case 1 rs.Open "select * from all_pages where keywords like '%" _
& strKeyword(0) & "%' and keywords like '%" & strKeyword(1) & _
"%' order by mydate desc", con
Case 2 rs.Open "select * from all_pages where keywords like '%" _
& strKeyword(0) & "%' and keywords like '%" & strKeyword(1) & _
"%' and keywords like '%" & strKeyword(2) & "%' order by _
mydate desc", con
Case Else rs.Open "select * from all_pages where keywords like '%" _
& strKeyword(0) & "%' and keywords like '%" & strKeyword(1) & _
"%' and keywords like '%" & strKeyword(2) & "%' order by _
mydate desc", con
End Select
We are using classic VBScript select statement to search the Search Engine database for the
keywords entered. Depending on the number of keywords entered we use different select statement
to search records. Here we are using Recordset object to open the database
and use the earlier created Connection as the connection string.
' If the returning recordset is not empty
If Not rs.EOF Then
records = records & "found"
rs.PageSize = recordsToShow
totalpages = rs.PageCount
rs.AbsolutePage = currentPage
If we have found some matching records in the database then set records
to "found". Then we set the PageSize property of Recordset
object to the number of records we want to show per page. We also get the number of total pages
available to show to the user in the variable totalpages. We then set the
AbsolutePage property of Recordset object to the
currentPage variable. We do this to keep track of which page we are showing
to the user and which page we should be showing.
' Showing total number of pages found and the current page number
Response.Write "Displaying Page " & currentPage & " of " & totalPages & "<br>"
Response.Write "Total Records Found : " & rs.RecordCount
Response.Write "<br><br>"
We show the user total number of pages of records available and total records found.
' Showing relevant records
Do Until rs.EOF
' Showing only 10 records at a time
If n = recordsToShow Then
Exit Do
End If
If InStr(1, urlshown, rs("url"), 1) = 0 Then
Response.Write "<b><a href=""" & rs("url") & """>" & rs("title") & _
"</a></b><br>" & vbcrlf
Response.Write "" & rs("description") & "<br>" & vbcrlf
Response.Write "URL : " & rs("url") & "</b><br>" & vbcrlf
Response.Write "Last indexed on : " & rs("mydate") & "<br><br>" & vbcrlf
n = n + 1
End If
urlshown = urlshown & " " & rs("url") & " "
rs.MoveNext
Loop
Now we start a Do Until loop to show all the available records found to the user while making
sure we don't show more records than the number of records specified in recordsToShow
variable.
For i = 0 To UBound(strKeyword)
strKeyword2 = strKeyword(i)
Next
look_for = strKeyword2
We now store the keywords to search in a variable look_for. We
will send the content of this variable to the next page so that when we are moving through
the sets of pages showing records, we know which keywords we should be searching.
' Links to move through the records
If currentPage > 1 Then
Response.Write "<a href=""" & Request.ServerVariables("SCRIPT_NAME") & _
"?currentPage=" & currentPage - 1 & "&look_for=" & look_for & _
""">Back</a>"
Else
Response.Write "<u style=""color : silver;"">Back</u>"
End If
Response.Write " "
If CInt(currentPage) <> CInt(totalPages) Then
Response.Write "<a href=""" & Request.ServerVariables("SCRIPT_NAME") & _
"?currentPage=" & currentPage + 1 & "&look_for=" & look_for & _
""">Next</a>"
Else
Response.Write "<u style=""color : silver;"">Next</u>"
End If
We now create 'Back' and 'Next' links to show accordingly if records are available to move
back and forth.
End If
' Done. Now release Objects
con.Close
Set con = Nothing
Set rs = Nothing
If InStr(1, records, "found", 1) = 0 Then
Response.Write "<b>Sorry, no matching record was found.</b>" & vbcrlf
End If
%>
</body></html>
In the end release the objects created during the execution of the page. If no matching records
were found then we inform the user that no matching records were found.
On the next page we will create the 'editme.asp' page.
Next ->
|