הצגה ג'נרית של רשומות

‏ • 20 ביולי, 2002



הרבה פעמים אנחנו מריצים שאילתות על בסיס הנתונים, מקבלים רקורדסט עם רשומות, ומציגים אותם על המסך בצורת טבלה.
מאמר זה מדגים קוד ג'נרי, המריץ שאילתה ובונה דינאמית את הטבלה, כולל הכותרות כאשר שמות השדות ברקורסדט, וכמותם יכול להשתנות בין שאילתה לשאילתה. הקוד יעיל במיוחד לצרכי בדיקות. ניתן באמצעותו להציג את תוכנה של טבלה, או את הרשומות המוחזרות משאילתה, ללא צורך בפתיחת הטבלה.

הגדרת המשתנים


1
2
3
4
5
6
7
8  

<%
Dim conn        ' connection object
Dim rs            ' RecordSet object
Dim mySQL        ' SQL Query
Dim showblank    ' string to display if the field is empty
Dim shownull    ' string to display if the field is NULL
%>
 

אתחול המשתנים והאובייקטים


1
2
3
4
5
6
7  

<%
mySQL="SELECT * FROM tbl"    
showblank="&nbsp;"
shownull="-null-"
Set conn=Server.CreateObject("ADODB.Connection")
%>
 

פתיחת אובייקט החיבור, והרצת השאילתה


1
2
3
4
5  

<%
conn.open "myDSN"
Set rs=conn.Execute(mySQL)
%>
 

אם השאילתה לא החזירה תוצאות, נציג הודעה מתאימה:


1
2
3
4
5
6  

<%
If rs.EOF then
   response.write "No records matched<br>"
Else
%>

הצגת כותרות הטבלה

באמצעות לולאה נעבור על כל השדות ברקורדסט, ונציג את שמם:


1
2
3
4
5
6
7
8  

<table border=1>
<tr>
<% 'Writing headers
For Each fld In rs.fields%>
    <td><b><%=fld.name%></B></TD>
<% next %>
</tr>

הצגת תוכן הטבלה

נעבור בלולאה עד סוף הרקורדסט ונבנה את השורות של הטבלה:


1
2  

<%Do While Not rs.EOF %>
     <tr>

עבור כל רשומה, נעבור על כל השדות שלה, נבדוק את תוכנו של כל שדה, לראות אם הוא ריק, או NULL, ונציג אותו:


1
2
3
4
5
6
7
8
9
10
11  

<% for each fld in rs.fields
  thisfield=fld.value
  if isnull(thisfield) then
     thisfield=shownull
  end if
  if trim(thisfield)="" then
     thisfield=showblank
  end if%>
 <td valign=top><%=thisfield%></td>
<% next %>
</tr>

בסופו של דבר, נסגור את האובייקטים.


1
2
3
4
5
6
7  


<%
rs.close
Set rs=nothing
conn.close
Set conn=nothing
%>

הקוד במלואו


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61  

<html>
<head>
<TITLE>TABLE</TITLE>
</head>
<body bgcolor="#FFFFFF">
<%

Dim conn        ' connection object
Dim rs            ' RecordSet object
Dim mySQL        ' SQL Query
Dim showblank    ' string to display if the field is empty
Dim shownull    ' string to display if the field is NULL

mySQL="SELECT * FROM tbl"    
showblank="&nbsp;"
shownull="-null-"
Set conn=server.createobject("adodb.connection")

conn.open "myDSN"
Set rs=conn.Execute(mySQL)

If rs.EOF then
   response.write "No records matched<br>"
Else%>
    <table border=1><tr>
    <% 'Writing headers
    For Each fld In rs.fields%>
         <td><b><%=fld.name%></B></TD>
    <% next %>
    </tr>

    <% ' Displaying records
    Do While Not rs.EOF %>
     <tr>
     <% for each fld in rs.fields
         thisfield=fld.value
         if isnull(thisfield) then
             thisfield=shownull
         end if
         if trim(thisfield)="" then
             thisfield=showblank
         end if%>
                 <td valign=top><%=thisfield%></td>
     <% next %>
     </tr>
     <%rs.MoveNext
    Loop%>
    </table>

    <%
End If
rs.close
Set rs=nothing
conn.close
Set conn=nothing
%>
</body></html>

בהצלחה!

תגיות: , , ,

תגובות בפייסבוק