A general database display function. The requirements are as follows:
1. Can display specified fields. Of course, the field names and displayed text can be different.
2. Can query multiple fields at the same time, supporting both fuzzy and precise query methods.
3. There are two display modes: horizontal arrangement and vertical arrangement of fields.
4. Can automatically paginate.
5. Can set how many records to display on each page. Well, that’s it for the big requirements. According to this requirement, Ayu wrote the following function.
In fact, Ayu wrote two functions here. One is TABDISP(), which is used to display the contents of a specified table. The other is ER(), a small error handling function.
This function has 5 parameters (that’s a lot). Each parameter must be entered with data and cannot be blank, otherwise an error will occur.
1(TAB), the name of the table to be called.
2 (DISPFILD), which fields in the table should be displayed, if the field names are different from the characters to be displayed, separate them with <. For example, a certain field name is: IMG, but it should be displayed as: Picture on the page, then, just write For IMG<pictures, if there are multiple fields, use "," to separate each field. Alas, it's a long story.
3(FINDFILD), which fields are to be queried, and the field names and characters to be displayed are separated by <.
4(PAGEN), how many records are displayed on each page.
5(FH), the display direction. When it is S, it is displayed vertically. When it is H, it is displayed horizontally.
The return value of the function is true when there is no error, otherwise it is false.
This function does not include the part of connecting to the database, so you have to connect to the library yourself. In addition, for repeated applications, you can save this function in a file and then include this file when needed.
Take a look at the following function: (Don’t miss reading it, I’ll give you an application example at the end)
<%
'The following defines a function TABDISP() for displaying tables, parameters TAB, the name of the table to be displayed, DISPFILD, the fields to be displayed, FINDFILD, the query field, PAGEN, the number displayed per page, fh, the direction of display
'If the display is successful, the return value is true; if it is not successful, the return value is false.
'Use, to separate multiple fields
'The query field must be included in the display field.
'If the field name is different from the name to be displayed, use < to separate the field name and the displayed name. The field name and display character must be separated by < for the field to be searched.
'FH display direction is "H" for horizontal display, "S" for vertical display, case-sensitive
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 "Current column: <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'>Accurate<input type='submit ' value='Start' 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)&"Contains ""&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 "After searching, the records of "&findlrdisp&" are as follows:"
Response.Write "<table border='0' width='90%'>"
if zs>=300 and topsn<>"" then
Response.Write "<tr> <form method='POST' action='?"&urllr&"'><td width='54%'>The total number is very large. The first "&ZS&" items are currently displayed, divided into "&PAGEZS&" pages. Now This is page "&PAGESN&"</td>"
else
Response.Write "<tr> <form method='POST' action='?"&urllr&"'><td width='54%'>There are "&ZS&" items in total, divided into "&PAGEZS&" pages. The current page is "&PAGESN&" page</td>"
end if
Response.Write "<td width='11%' align='center'><a href='?PAGESN="&PAGESN-1&urllr&"'>Previous page</A></td>"
Response.Write "<td width='11%' align='center'><a href='?PAGESN="&PAGESN+1&urllr&"'>Next page</A></td>"
Response.Write "<td width='24%' ALIGN='RIGHT'>Jump to page"
Response.Write "<input type='text' name='PAGESN' size='2'>Page<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%'>The total number is very large. The first "&ZS&" items are currently displayed, divided into "&PAGEZS&" pages. Now This is page "&PAGESN&"</td>"
else
Response.Write "<tr> <form method='POST' action='?"&urllr&"'><td width='54%'>There are "&ZS&" items in total, divided into "&PAGEZS&" pages. The current page is "&PAGESN&" page</td>"
end if
Response.Write "<td width='11%' align='center'><a href='?PAGESN="&PAGESN-1&urllr&"'>Previous page</A></td>"
Response.Write "<td width='11%' align='center'><a href='?PAGESN="&PAGESN+1&urllr&"'>Next page</A></td>"
Response.Write "<td width='24%' ALIGN='RIGHT'>Jump to page"
Response.Write "<input type='text' name='PAGESN' size='2'>Page<input type='submit' value='GO' name='B1'></td></form> "
Response.Write "</tr></table> "
else
Response.Write "No suitable record found"
end if
if er() then
tabdisp=False
else
tabdisp=True
end if
END FUNCTION
Function Er() 'Error handling function
If Err.Number = 0 Then
Er=False
Else
Err.Clear
Er=True
End If
End Function
%>
Finally finished reading the function...
You are a little tired, but it doesn’t matter if you look carefully or not. Anyway, just adjust it. Here is an application example.
First, we save the previous function, which is the part between <% and %>, into a file called TABDISP.ASP.
The code is as follows:
Display a job roster table, table name: ZG
There are several fields including ID, name, job title, TEL BP, DZ (address).
<%
. . . . . .
Connecting to the database is skipped here and will not be written.
%>
<!--#include file="TABDISP.asp"-->
<%
DISP="name,position,title,TEL<phone,BP<paging,DZ<address"
FIND="Name<Name, Position<Title, DZ<Address"
PAGEN="15"
FH="H"
TAB="ZG"
IF NOT TABDISP(TAB,DISP,FIND,PAGEN,FH) THEN
response.write "An error occurred,"
END IF
SET CONN=NOTHING
%>
Just give the above content a file name and everything will be fine.