阿余经常写一些数据库相关的程序,当然离不开显示库中的数据了,说实话,做这样的程序真是无聊啊,所以,阿余就想写个函数,一个通用的数据库显示函数.要求如下:
1. 能显示指定的字段,当然,字段名和显示的文字可以不一样.
2. 能同时按多个字段进行查询,支持模糊和精确两种查询方式.
3. 有横向排列和纵向排列字段两种显示方式.
4. 能自动分页.
5. 能设定每页显示多少条记录.好啦,要求大至就是这样了.根据这个要求,阿余写了下面的函数.
实际上,这里阿余写了两个函数.一个是TABDISP(),用于显示一个指定表中的内容.还有一个是ER()一个小小的错误处理函数.
这个函数有5个参数(真够多的).并且各个参数都要输入数据,不能是空白,不然会出错的.
1(TAB), 要调用的表名.
2(DISPFILD), 要显示表中的哪一些字段,如字段名和要显示的字符不一样用<分隔.比如某一个字段名为:IMG,但要在页面上显示为:图片, 那么,就写为IMG<图片,如果有多个字段,各字段间用“,”分隔,唉,老声长谈.
3(FINDFILD), 要按哪些字段进行查询,字段名和要显示的字符间用<分隔.
4(PAGEN), 每页显示多少条记录.
5(FH), 显示的方向.当为S时纵向显示.为H时横向显示.
函数的返回值,当没有出错的时候,返回值为真,否则为假.
这个函数中没有包括连接到数据库的部分.所以要自己连接到库.另外,为了重复应用.可以把这个函数专门用一个文件来保存,然后在需要的时候包含这个文件.
看下面的函数: (不要不看完,最后我举有应用的例子)
1
2'以下定义一个显示表格的函数TABDISP(),参数TAB,被显示的表名,DISPFILD,要显示的字段,FINDFILD,查询字段,PAGEN,每页显示数量,fh,显示的方向
3'如显示成功,返回值为真,不成功,返回值为假.
4'多个字段间用,分隔
5'查询字段一定要包含在显示字段中.
6'如字段名与要显示的名称不一样,用<分隔,前面是字段名,后面是显示的名字,而要进行搜索的字段则一定要用<分隔要字段名和显示字符
7'FH显示方向为"H"时作横向显示,为"S"时作纵向显示 区分大小写
8FUNCTION TABDISP(TAB,DISPFILD,FINDFILD,PAGEN,FH)
9On Error Resume Next
10IF DISPFILD="" THEN DISPFILD="*"
11IF PAGEN="" THEN PAGEN=15
12DISPFILD1=DISPFILD&","
13findfild1=findfild&","
14
15dim findl(10),findr(10)
16i1=1
17do while instr(findfild1,",")<>0
18star=instr(findfild1,",")
19findl(i1)=left(findfild1,star-1)
20findr(i1)=findl(i1)
21fildbak=findl(i1)
22findl(i1)=left(fildbak,instr(fildbak,"<")-1)
23findr(i1)=right(fildbak,len(fildbak)-instr(fildbak,"<"))
24findfild1=right(findfild1,len(findfild1)-star)
25i1=i1+1
26loop
27
28Response.Write "
<table border="0" cellspacing="0" width="100%"><tr><form method="GET">"
Response.Write "<td> " 'width='50%'
IF SESSION("FLMENULBUP")<>"" THEN
Response.Write "当前栏目:<a href='FLMEN.ASP?MENULB="&SESSION("FLMENULBUP")&"'>"&MID(SESSION("FLMENULBUP"),4,200)&"</a>"
END IF
IF SESSION("flmenulb")<> SESSION("FLMENULBUP") THEN
Response.Write " >> <a href='FLMEN.ASP?menulb="&SESSION("FLMENULBUP")&"&LB="&SESSION("flmenulb")&"'>"&MID(SESSION("flmenulb"),4,200)&"</a>"
END IF
Response.Write "</td><td align="right"></td><td align="right"><table border="0" cellpadding="0" cellspacing="0"> "
Response.Write "<tr><td rowspan="2"><img src="img/SEARCH.GIF" width="50"/> </td>"
for i3=1 to i1-1
Response.Write "<td>"&findr(i3)&":</td>"
next
Response.Write "</tr><tr>" '<td></td>
for i2=1 to i1-1
Response.Write "<td><input name='key_word"&i2&"' size="8" type="text" value=""/></td>"
next
Response.Write "<tr></tr></tr></table></td><td width="20%"> <input name="find_fr" type="checkbox" value="yes"/>精确 <input name="B_FIND" type="submit" value="开始"/></td>"
Response.Write "</form> </tr></table>
1"
2find_fr=request("find_fr")
3TJ=""
4urllr="&find_fr="&find_fr
5findlrdisp=""
6FOR I3=1 TO I1-1
7TJ1=request("KEY_WORD"&I3)
8urllr=urllr&"&key_word"&i3&"="&tj1
9IF TJ1=FINDR(I3) OR TJ1="" THEN
10TJ1=""
11ELSE
12if find_fr<>"yes" then
13findlrdisp=findlrdisp&findr(i3)&"包含“"&TJ1&"” "
14tj1=" AND "&findL(I3)&" like '%"&TRIM(TJ1)&"%' "
15else
16findlrdisp=findlrdisp&findr(i3)&"是“"&TJ1&"” "
17tj1=" AND "&FINDL(I3)&" = '"&TRIM(TJ1)&"' "
18end if
19END IF
20TJ=TJ&TJ1
21IF TJ="" THEN
22TOPSN=" TOP 300 "
23ELSE
24TOPSN=""
25END IF
26NEXT
27
28dim fild(35),dispfil(35),dispfildlr
29dispfildlr=""
30i=1
31dispfild1=dispfild1
32do while instr(dispfild1,",")<>0
33star=instr(dispfild1,",")
34fild(i)=left(dispfild1,star-1)
35dispfil(i)=fild(i)
36if instr(fild(i),"<")<>0 then
37fildbak=fild(i)
38fild(i)=left(fildbak,instr(fildbak,"<")-1)
39dispfil(i)=right(fildbak,len(fildbak)-instr(fildbak,"<"))
40end if
41dispfildlr=dispfildlr&fild(i)&","
42dispfild1=right(dispfild1,len(dispfild1)-star)
43i=i+1
44loop
45dispfildlr=left(dispfildlr,len(dispfildlr)-1)
46SQL="SELECT "&topsn&" id,"&DISPFILDlr&" FROM "&TAB&" WHERE ID<>-1 "&tj&" order by -ID"
47'Response.Write sql
48session("tabsql")=sql
49Set rs=Server.CreateObject("ADODB.RecordSet")
50rs.Open sql,conn,1,3
51if not rs.eof then
52pagesn=request("pagesn")
53if pagesn<=0 or pagesn="" then pagesn=1
54rs.pagesize=pagen
55pagezs=rs.pagecount
56IF cint(PAGESN)>pagezs THEN pagesn=PAGEZS
57zs=rs.recordcount
58page=(pagesn-1)*pagen
59rs.move page,1
60if findlrdisp<>"" then Response.Write "经搜索,"&findlrdisp&"的记录如下:"
61Response.Write "
<table border="0" width="90%">"
if zs>=300 and topsn<>"" then
Response.Write "<tr> <form action='?"&urllr&"' method="POST"><td width="54%">总数很多,当前显示前"&ZS&"条,分"&PAGEZS&"页,现在是第"&PAGESN&"页</td>"
else
Response.Write "<tr> <form action='?"&urllr&"' method="POST"><td width="54%">一共"&ZS&"条,分"&PAGEZS&"页,现在是第"&PAGESN&"页</td>"
end if
Response.Write "<td align="center" width="11%"><a href='?PAGESN="&PAGESN-1&urllr&"'>上一页</a></td>"
Response.Write "<td align="center" width="11%"><a href='?PAGESN="&PAGESN+1&urllr&"'>下一页</a></td>"
Response.Write "<td align="RIGHT" width="24%">跳转到第 "
Response.Write "<input name="PAGESN" size="2" type="text"/>页<input name="B1" type="submit" value="GO"/></td></form> "
Response.Write "</tr></form></tr></table>
1"
2if fh="H" THEN
3Response.Write "
<table border="0" cellpadding="0" cellspacing="1" width="100%">"
Response.Write " <tr align="center" bgcolor="#EDBAA5"> "
for ii=1 to i-1
Response.Write "<td>"&dispFIL(ii)&"</td>"
next
Response.Write "</tr>"
for iii=1 to pagen
Response.Write "<tr bgcolor="#FFFAEE">"
FOR II=1 TO I-1
Response.Write "<td>"&RS(fild(II))&"</td>"
NEXT
Response.Write "</tr>"
RS.MOVENEXT
if rs.eof then exit for
next
Response.Write "</table>
1"
2ELSE
3Response.Write "
<table border="0" cellpadding="0" cellspacing="1" width="100%">"
for iii=1 to pagen
FOR II=1 TO I-1
Response.Write "<tr><td align="RIGHT" bgcolor="#EDBAA5" width="20%">"&dispFIL(ii)&":</td><td bgcolor="#FFFAEE">"&RS(fild(II))&"</td></tr>"
NEXT
Response.Write "<tr height="1"><td align="RIGHT" bgcolor="#CFBA9E" colspan="2" width="20%"> </td></tr>"
RS.MOVENEXT
if rs.eof then exit for
next
Response.Write "</table>
1"
2END IF
3rs.close
4Response.Write "
<table border="0" width="90%">"
if zs>=300 and topsn<>"" then
Response.Write "<tr> <form action='?"&urllr&"' method="POST"><td width="54%">总数很多,当前显示前"&ZS&"条,分"&PAGEZS&"页,现在是第"&PAGESN&"页</td>"
else
Response.Write "<tr> <form action='?"&urllr&"' method="POST"><td width="54%">一共"&ZS&"条,分"&PAGEZS&"页,现在是第"&PAGESN&"页</td>"
end if
Response.Write "<td align="center" width="11%"><a href='?PAGESN="&PAGESN-1&urllr&"'>上一页</a></td>"
Response.Write "<td align="center" width="11%"><a href='?PAGESN="&PAGESN+1&urllr&"'>下一页</a></td>"
Response.Write "<td align="RIGHT" width="24%">跳转到第 "
Response.Write "<input name="PAGESN" size="2" type="text"/>页<input name="B1" type="submit" value="GO"/></td></form> "
Response.Write "</tr></form></tr></table>
1"
2else
3Response.Write "没有找到合适的记录"
4end if
5if er() then
6tabdisp=False
7else
8tabdisp=True
9end if
10END FUNCTION
11
12Function Er() '错误处理函数
13If Err.Number = 0 Then
14Er = False
15Else
16Err.Clear
17Er = True
18End If
19End Function
终于看完函数了....
各位有点累,其实仔不仔细看都没有关系.反正只管调它就是了.下面举一个应用的例子.
首先, 我们把前面的函数就是```
和
1代码如下:
2显示一个职式名册表,表名: ZG
3有ID 姓名 职务 职称 TEL BP DZ(地址)几个字段.
4
。。。。。。
连接数据库,此处略过,不写了。
1<!--#include file="TABDISP.asp"-->
DISP="姓名,职务,职称,TEL<电话,BP<传呼,DZ<地址"
FIND="姓名<姓名,职务<职务,DZ<地址"
PAGEN="15"
FH="H"
TAB="ZG"
IF NOT TABDISP(TAB,DISP,FIND,PAGEN,FH) THEN
response.write "出现错误,"
END IF
SET CONN=NOTHING
1
2把上面的内容随便起个文件名,就一切OK.
3
4另外, 阿余的站www.zydn.net有不少好文章给大家.阿余也在站上随时准备和和朋友交流.阿余的EMAIL:[email protected]
5还有啊,软件世界竟然发了篇牛记的文章,说程序员工资太高,阿余在站上也放了个论坛,大家可以一起到阿余的站上去骂死那个家伙.