悉远

 找回密码
 注册
搜索
查看: 2461|回复: 1

一个通用数据库显示程序,能调任意库,任意字段,多关键字搜索,自动分页

[复制链接]
发表于 2009-6-22 23:25:16 | 显示全部楼层 |阅读模式
一个通用的数据库显示函数.要求如下:
1. 能显示指定的字段,当然,字段名和显示的文字可以不一样.
2. 能同时按多个字段进行查询,支持模糊和精确两种查询方式.
3. 有横向排列和纵向排列字段两种显示方式.
4. 能自动分页.
5. 能设定每页显示多少条记录.好啦,要求大至就是这样了.根据这个要求,阿余写了下面的函数.
实际上,这里阿余写了两个函数.一个是TABDISP(),用于显示一个指定表中的内容.还有一个是ER()一个小小的错误处理函数.
这个函数有5个参数(真够多的).并且各个参数都要输入数据,不能是空白,不然会出错的.
1(TAB), 要调用的表名.
2(DISPFILD), 要显示表中的哪一些字段,如字段名和要显示的字符不一样用<分隔.比如某一个字段名为:IMG,但要在页面上显示为:图片, 那么,就写为IMG<图片,如果有多个字段,各字段间用“,”分隔,唉,老声长谈.
3(FINDFILD), 要按哪些字段进行查询,字段名和要显示的字符间用<分隔.
4(PAGEN), 每页显示多少条记录.
5(FH), 显示的方向.当为S时纵向显示.为H时横向显示.
函数的返回值,当没有出错的时候,返回值为真,否则为假.
这个函数中没有包括连接到数据库的部分.所以要自己连接到库.另外,为了重复应用.可以把这个函数专门用一个文件来保存,然后在需要的时候包含这个文件.
看下面的函数: (不要不看完,最后我举有应用的例子)
<%
'以下定义一个显示表格的函数TABDISP(),参数TAB,被显示的表名,DISPFILD,要显示的字段,FINDFILD,查询字段,PAGEN,每页显示数量,fh,显示的方向
'如显示成功,返回值为真,不成功,返回值为假.
'多个字段间用,分隔
'查询字段一定要包含在显示字段中.
'如字段名与要显示的名称不一样,用<分隔,前面是字段名,后面是显示的名字,而要进行搜索的字段则一定要用<分隔要字段名和显示字符
'FH显示方向为"H"时作横向显示,为"S"时作纵向显示 区分大小写
FUNCTION  TABDISP(TAB,DISPFILD,FINDFILD,PAGEN,FH)
  On Error Resume Next
  IF DISPFILD="" THEN DISPFILD="*"
  IF PAGEN="" THEN PAGEN=15
  DISPFILD1=DISPFILD&","
  findfild1=findfild&","
  dim findl(10),findr(10)
  i1=1
  do while instr(findfild1,",")<>0
    star=instr(findfild1,",")
    findl(i1)=left(findfild1,star-1)
    findr(i1)=findl(i1)
    fildbak=findl(i1)
    findl(i1)=left(fildbak,instr(fildbak,"<")-1)
    findr(i1)=right(fildbak,len(fildbak)-instr(fildbak,"<"))
    findfild1=right(findfild1,len(findfild1)-star)
    i1=i1+1
  loop
   
  Response.Write "<table border='0' width='100%' cellspacing='0'><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' cellspacing='0' cellpadding='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 type='text' name='key_word"&i2&"' size='8' value=''></td>"
  next
  Response.Write "<tr></table></td><td width='20%'> <input type='checkbox' name='find_fr' value='yes'>精确 <input type='submit' value='开始' name='B_FIND'></td>"
  Response.Write "</form> </tr></table>"
  find_fr=request("find_fr")
  TJ=""
  urllr="&find_fr="&find_fr
  findlrdisp=""
  FOR I3=1 TO I1-1
     TJ1=request("KEY_WORD"&I3)
     urllr=urllr&"&key_word"&i3&"="&tj1
     IF TJ1=FINDR(I3) OR TJ1="" THEN  
       TJ1=""
     ELSE
        if find_fr<>"yes" then
          findlrdisp=findlrdisp&findr(i3)&"包含“"&TJ1&"” "
          tj1=" AND "&findL(I3)&" like '%"&TRIM(TJ1)&"%' "
        else
           findlrdisp=findlrdisp&findr(i3)&"是“"&TJ1&"” "
           tj1=" AND "&FINDL(I3)&" = '"&TRIM(TJ1)&"' "
        end if
     END IF
     TJ=TJ&TJ1
     IF TJ="" THEN
        TOPSN=" TOP 300 "
     ELSE
        TOPSN=""
     END IF
  NEXT
  dim fild(35),dispfil(35),dispfildlr
  dispfildlr=""
  i=1
  dispfild1=dispfild1
  do while instr(dispfild1,",")<>0
  star=instr(dispfild1,",")
  fild(i)=left(dispfild1,star-1)
  dispfil(i)=fild(i)
  if instr(fild(i),"<")<>0 then
      fildbak=fild(i)
      fild(i)=left(fildbak,instr(fildbak,"<")-1)
      dispfil(i)=right(fildbak,len(fildbak)-instr(fildbak,"<"))
  end if
  dispfildlr=dispfildlr&fild(i)&","
  dispfild1=right(dispfild1,len(dispfild1)-star)
  i=i+1
  loop
  dispfildlr=left(dispfildlr,len(dispfildlr)-1)
   SQL="SELECT  "&topsn&"  id,"&DISPFILDlr&"    FROM "&TAB&"  WHERE ID<>-1 "&tj&"  order by -ID"
    'Response.Write sql
    session("tabsql")=sql
    Set rs=Server.CreateObject("ADODB.RecordSet")  
    rs.Open sql,conn,1,3
    if not rs.eof then
    pagesn=request("pagesn")
    if pagesn<=0 or pagesn="" then pagesn=1
    rs.pagesize=pagen
    pagezs=rs.pagecount
    IF cint(PAGESN)>pagezs THEN pagesn=PAGEZS
       zs=rs.recordcount
       page=(pagesn-1)*pagen   
       rs.move page,1  
       if findlrdisp<>"" then Response.Write "经搜索,"&findlrdisp&"的记录如下:"
       Response.Write  "<table border='0' width='90%'>"
       if zs>=300 and topsn<>"" then
          Response.Write  "<tr>  <form method='POST'   action='?"&urllr&"'><td width='54%'>总数很多,当前显示前"&ZS&"条,分"&PAGEZS&"页,现在是第"&PAGESN&"页</td>"
       else
          Response.Write  "<tr>  <form method='POST'   action='?"&urllr&"'><td width='54%'>一共"&ZS&"条,分"&PAGEZS&"页,现在是第"&PAGESN&"页</td>"
       end if
       Response.Write  "<td width='11%' align='center'><a href='?PAGESN="&PAGESN-1&urllr&"'>上一页</A></td>"
       Response.Write  "<td width='11%' align='center'><a href='?PAGESN="&PAGESN+1&urllr&"'>下一页</A></td>"
       Response.Write  "<td width='24%' ALIGN='RIGHT'>跳转到第 "
       Response.Write  "<input type='text' name='PAGESN' size='2'>页<input type='submit' value='GO' name='B1'></td></form> "
       Response.Write  "</tr></table> "
       if fh="H" THEN
       Response.Write  " <table border='0' width='100%' cellspacing='1' cellpadding='0'>"
       Response.Write  " <tr bgcolor='#EDBAA5' align='center'> "
       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>"
    ELSE
       Response.Write  " <table border='0' width='100%' cellspacing='1' cellpadding='0'>"
       for iii=1 to pagen
           FOR II=1 TO I-1
             Response.Write "<tr><td bgcolor='#EDBAA5' ALIGN='RIGHT' WIDTH='20%'>"&dispFIL(ii)&":</td><td bgcolor='#FFFAEE'>"&RS(fild(II))&"</td></tr>"  
           NEXT
           Response.Write "<tr  height='1'><td bgcolor='#CFBA9E'  colspan='2' ALIGN='RIGHT' WIDTH='20%'> </td></tr>"  
        RS.MOVENEXT
        if rs.eof then exit for
      next
      Response.Write "</table>"
    END IF
  rs.close
  Response.Write  "<table border='0' width='90%'>"
if zs>=300 and topsn<>"" then
  Response.Write  "<tr>  <form method='POST'   action='?"&urllr&"'><td width='54%'>总数很多,当前显示前"&ZS&"条,分"&PAGEZS&"页,现在是第"&PAGESN&"页</td>"
else
  Response.Write  "<tr>  <form method='POST'   action='?"&urllr&"'><td width='54%'>一共"&ZS&"条,分"&PAGEZS&"页,现在是第"&PAGESN&"页</td>"
end if
  Response.Write  "<td width='11%' align='center'><a href='?PAGESN="&PAGESN-1&urllr&"'>上一页</A></td>"
  Response.Write  "<td width='11%' align='center'><a href='?PAGESN="&PAGESN+1&urllr&"'>下一页</A></td>"
  Response.Write  "<td width='24%' ALIGN='RIGHT'>跳转到第 "
  Response.Write  "<input type='text' name='PAGESN' size='2'>页<input type='submit' value='GO' name='B1'></td></form> "
  Response.Write  "</tr></table> "
  else
      Response.Write  "没有找到合适的记录"
  end if
if er() then
  tabdisp=False
else
  tabdisp=True
end if
END FUNCTION
Function Er()  '错误处理函数
        If Err.Number = 0 Then
        Er = False
        Else
        Err.Clear
        Er = True
        End If
End Function
%>

    终于看完函数了....
各位有点累,其实仔不仔细看都没有关系.反正只管调它就是了.下面举一个应用的例子.
首先, 我们把前面的函数就是<%和%>之间的部分保存到一个叫 TABDISP.ASP的文件中.
代码如下:
显示一个职式名册表,表名: ZG  
有ID 姓名 职务 职称 TEL BP DZ(地址)几个字段.
<%
。。。。。。
连接数据库,此处略过,不写了。
%>
<!--#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
%>

把上面的内容随便起个文件名,就一切OK.
发表于 2010-2-27 19:17:07 | 显示全部楼层
先顶后看  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|QQ客服|联系我们|Archiver|手机版|小黑屋|悉远网络 ( 鄂ICP备09013446号 )

GMT+8, 2024-3-28 19:05 , Processed in 0.033699 second(s), 9 queries , Redis On.

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表