In this part, you will create full-text search capability to provide an index for the magazine.
If you don't already have the Magazine project open, open it now. Choose New from the File menu. In the New dialog box, create a new ASP page, name it SEARCH.ASP, and click OK.
Open DEFAULT.ASP in Visual InterDev Editor. You need to add a form to this page to access the SEARCH.ASP page. A search string should be entered in a text field and passed to the search engine. Add the following code to the BODY section to place a hyperlink in the page:
<!-- Index search form --> <DIV STYLE="position:absolute; top:250px; left:400px; width:200;"> <CENTER> <IMG SRC="/Magazine/Images/Search.gif"><BR> <FORM ACTION="/Magazine/Search.asp" METHOD="POST"> <INPUT NAME="SearchString"><BR> <INPUT TYPE="SUBMIT" VALUE="Search!"> </FORM> </CENTER> </DIV>
Save and close DEFAULT.ASP.
Open SEARCH.ASP, your new search engine for the site. In this page, you will open a connection to the database, provide a text search mechanism, and build a list of hyperlinks to the appropriate documents.
Start by linking SEARCH.ASP to the site style sheet to maintain a consistent look. Add the following code to the HEAD section of SEARCH.ASP:
<LINK REL=STYLESHEET HREF="Magazine.css">
The actual search is performed by creating a query against the database, using the keyword submitted by the user. If the keyword appears in an article's text, a hyperlink is generated in the results page. In this step, you will use ADO to run the query and show the results of the search.
NOTE: The SQL statement for this exercise can be tricky to write. Although spread out across several lines of code, an example of the final statement might look like this:
SELECT * FROM Articles, Sections WHERE Sections.ArticleID = Articles.ArticleID AND Sections.SectionText LIKE `%VBScript%'
Add the following code to the BODY section of SEARCH.ASP to complete the page:
<H1>Here are the results of your search...</H1> <% ` Variables Dim objConnection Dim objRecordset Dim strSQL ` Open data source Set objConnection = Server.CreateObject("ADODB.Connection") objConnection.Open "Magazine" ` Build SQL statement strSQL = "SELECT * FROM Articles,Sections " strSQL = strSQL & "WHERE Sections.ArticleID = Articles.ArticleID " strSQL = strSQL & "AND Sections.SectionText LIKE `%" & _ Request.Form("SearchString") & "%'" ` Get records Set objRecordset = Server.CreateObject("ADODB.Recordset") objRecordset.Open strSQL, objConnection%> <TABLE> <% Do While Not objRecordset.EOF %> <TR> <TD> <A HREF="Content.asp?ArticleID=<%=objRecordset("ArticleID")%>"> <H3><%=objRecordset("ArticleTitle")%></H3> </A> </TD> </TR> <% ' Get next record objRecordset.MoveNext Loop %> </TABLE>
Save your work, and test the site. Try searching for a keyword. Your result should look like Figure 8-8.
Figure 8-8. Search results.