如何用ASP做个站内搜索?

小弟,急需做一个站内搜索,盼望各位大侠想救!
---------------------------------------------------------------

body区:

 1<script language="JavaScript">   
 2  
 3<!--   
 4  
 5// 下面是负责搜索的页面的全路径或相对路径   
 6  
 7var search_htm_url = "searchsource.htm";   
 8  
 9  
10  
11function searchPage() {   
12  
13if ((document.searchpage.keyword.value.length == 0)   
14  
15¦ ¦ (document.searchpage.keyword.value == " ")) {   
16  
17alert("请填入关键字!");   
18  
19} else {   
20  
21sel = document.searchpage.and_or.selectedIndex;   
22  
23location.href = search_htm_url + "?"   
24  
25\+ escape(document.searchpage.keyword.value)   
26  
27\+ (sel==0?"&and":(sel==2?"&exact":"&or"));   
28  
29}   
30  
31return false;   
32  
33}   
34  
35  
36  
37document.write('<form name="searchpage" onSubmit="return searchPage()">'   
38  
39+'请填入关键字: <input type="text" size=22 name="keyword"> '   
40  
41+'<input type="button" value="Search" onClick="searchPage()"><br>'   
42  
43+'<select name="and_or" size=1><option>关键字与搜索<option>关键字或搜索 '   
44  
45+'<option>关键字异或搜索</select></form>');   
46  
47// -->   
48  
49</script>

---------------------------------------------------------------

假如你拥有一个庞大的网站,内容又多,那么来访者往往很难找到自己所需要的东东,这时候你就需要一个站内搜索来帮助来访者更快的找到索要的资料了!现在你就可以用asp轻易的实现这种功能,何况现在支持asp的站点这么多,利用这个搜索引擎可以搜索到你的主页里面任何一个文件或者软件资料,它可以精确到每个字!说了这么多你是不是怦然心动了啊,那就按照下面的步骤来建立自己的搜索引擎吧:

(一),首先利用access97建立一个名为list.mdb的数据库,在里面建立一个名为list的表,然后在list表里面输入一些你想要被查询的文件名字,关键的搜索词还有相对应的链接,下面建立了四个项目ID(编号),title(主题),word(关键词),url(链接地址),如下表所示:

ID title word url
1 cgi教程 本地调试cgi cgi/testcgi.htm
2 下载中心 聊天室,留言本 download/index.htm

(二),建立了数据库以后就可以建立你的asp搜索页面了,下面是一个文件名为search.htm的asp搜索页面的源程序,这个搜索引擎可以同时搜索title和word里面的内容,当然如果你有需要的话可以建立更多的搜索:

1 "建立数据库的连接   
2con="DBQ="+server.mappath("list.mdb")+";DefaultDir=;DIRVER={microsoft Access Driver(*.mdb)};"   
3"建立CONNECTION对象并打开数据库   
4set mycon=server.createobject("ADODB.CONNECTION")   
5mycon.open.con 

"创建Recordset对象的例程,打开Recordset对象传递SQL串以及所有的连接信息

1 set rs=server.createobject("ADODB.Recordset")   
2rs.open "SELECT*FORM list where title and word like '%'&request.form("word")&"%'",MyCon,adOpenStatie   

"建立查询后的显示信息和查询页面,你想取什么名字都可以

 1<html><head><title>查询结果</title></head>
 2<body bgcolor="#ffffff">
 3<p align="center"><br/>   
 4"统计查询共有```
 5 Response.Write(RS.RecordCount) 
 6```条纪录</p><br/>
 7<div align="center"><center>
 8<table align="center" berdorcoorlight="#000000" border="1" bordercolor="000000" bordercolordark="#ffffff">
 9<tr align="center">
10<td align="center" bgcolor="#ffffff" width="20%">ID</td>
11<td align="center" bgcolor="#ffffff" width="60%">主题</td>
12<td align="center" bgcolor="#ffffff" width="20%">查看</td></tr></table>
13<table align="center" berdorcoorlight="#000000" border="1" bordercolor="000000" bordercolordark="#ffffff">
14<tr align="center">
15<!--从数据库提取信息-->   

while not rs.eof

1<tr align="center">
2<td align="center" bgcolor="#ffffff" width="20%">```
3 =rs("id") 
4```</td>
5<td align="center" bgcolor="#ffffff" width="60%">```
6 =rs("title") 
7```</td>
8<td align="center" bgcolor="#ffffff" width="20%"><a ```"="" href="```
9 =rs(" url")="">GO</a></td>   

rs.movenext </tr> wend </tr></table></center></div> rs.close

mycon.close

1"关闭链接   
2</body></html>

下面我们来建立搜索页面

 1<html><head><title>搜索页面</title></head>
 2<!--创建搜索表格-->
 3<from action="search.asp" method="POST">
 4<div align="center"><center><p><input name="word" size="30" type="text"/>
 5<input name="B1" type="submit" value="提交"/><input name="B2" type="reset" value="清除"/></p>
 6</center></div>
 7<hr align="center" width="600"/>
 8<html>   
 9好了,这个站内搜索引擎到这里就算完成了,你接下来要做的就是吧东西输入数据库了!   
10  
11\---------------------------------------------------------------   
12  
13可以利用Index Server 参考 www.aspsky.com 的代码   
14\---------------------------------------------------------------   
15  
16http://www.aspsky.net/article/list.asp?id=2092   
17http://www.aspsky.net/article/list.asp?id=2273   
18  
19  
20.NET 写错了,不好意思   
21\---------------------------------------------------------------   
22  
23站内搜索主要是根据已经有的表的字段进行搜索,数据库要造好   
24接下来很简单的,只是个form加sql语句   
25\---------------------------------------------------------------   
26  
27利用ASP打造站内搜索引擎   
28  
29http://www.ccw.com.cn/htm/app/aprog/02_1_23_2.asp   
30\---------------------------------------------------------------   
31  
32配置Index Server   
33在执行Web服务器上的搜索之前,首先必须创建至少一个索引,并遵循以下步骤完成这项工作。   
34  
35⒈启动Windows 2000 Server服务器上的索引。   
36  
37缺省情况下选择图标位于管理工具组中的计算机管理。窗口的右边提供关于当前在服务器上   
38存在的索引信息。默认有两个索引:System和Web。   
39  
40⒉要创建新的索引,用鼠标右键单击索引服务或右边的面板,选定新建编录。   
41  
42显示添加编录对话框,指定索引的名称并用浏览按钮选取位置。索引服务不会立即开始索引,此时将弹出一条消息框,单   
43击确定继续。为了达到最佳的性能,索引服务可以放在和Web服务器隔离开的硬盘上。   
44  
45⒊指定索引的目录,用右键单击新建的编录名,从弹出选单中选定属性,将出现如图2所示的对话框。   
46  
47第一个选项卡常规显示刚刚输入的内容,在第二个选项卡跟踪中的WWW服务器下拉列表中选取你要索引的Web网站。   
48  
49⒋有了创建的编录,现在可以挑选你想在索引中包括的目录。   
50  
51这个强大的特性使你能创建几个用不同方式搜索内容的索引。例如,你可能想快速浏览从完整的文本中所引出的章节,这   
52个特性使你能在Index Server内执行这项任务。要添加一个目录,用右键单击右边的面板,选定新建目录。在出现的对话   
53框中通过浏览按钮选择目录并指定别名。如果必须登录到服务器才能得到需要的内容,则可以提供将使用的用户名和密   
54码,以便得到需要的内容。如果想检查受保护的内容,则更要维护内容的安全性,这一特性非常有用,这样只有被授权的   
55用户(也许是那些为此付钱的人)才能得到完整的内容。   
56  
57还可以使用这个工具排除Web站点中不想索引的子目录。例如,检索Scripts/CGI目录就不会对用户有什么用。为此,只要   
58双击该目录,将包括在索引中选定为否即可,见图3。   
59  
60⒌在已经为站点包括/排除了所有目录后,用右键单击右面板,从弹出选单中选定启动,重新启动Index Server服务。   
61  
62服务器将开始浏览已经创建的索引并开始检索内容。   
63  
64源程序:   
651、search.asp   
66<html>
67<head>
68<title>The Indexing Service</title>
69<style type="text/css">   
70BODY {font-family:Tahoma,Arial,sans-serif; font-size:10pt}   
71.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}   
72.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}   
73</style>
74</head>
75<body bgcolor="#FFFFFF">
76<span class="heading">Searching The Indexing Service Using ADO</span><hr/>
77<!--------------------------------------------------------------------------->
78<form action="QueryIndexServer.asp" method="POST" name="frmSearch">   
79Search For: <input name="txtSearchFor" type="TEXT"/>
80<p>
81<input type="SUBMIT" value="Perform Search"/>
82<input type="RESET" value="Clear Form"/>
83</p></form>
84</body>
85</html>   
862、QueryIndexServer.asp   
87<title>The Indexing Service</title>
88<style type="text/css">   
89BODY {font-family:Tahoma,Arial,sans-serif; font-size:10pt}   
90.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}   
91.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}   
92.document {font-size:10pt; font-weight:bold; background-color:lightgrey; width:100%}   
93</style>
94
95<body bgcolor="#FFFFFF">
96<span class="heading">Res</span></body></html></from></html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus