在经典asp中打印数据,如单行,多列?

时间:2015-10-27 11:23:52

标签: asp-classic

就像在第1列到第30列中打印的日期一样,我需要员工登录时间并在单日列中注销时间...```

我需要在表格中打印数据,例如在经典asp

中有多列的单行
<form name="frm_display" method="post">
Welcome: <%=Session("emp_fname")%>
<br>ID: <%=Session("emp_id")%>
<% 
dim con 
dim query 
dim rs2 
set con = Server.CreateObject("ADODB.Connection")
set rs2 = Server.CreateObject( "ADODB.RECORDSET")
con.Open "Provider=SQLOLEDB.1;Password=swtest.net;Persist Security Info=True;User ID=sa;Initial Catalog=hr;Data Source=172.16.1.208" 
%>
<table align="center">
    <tr>
        <td>Month:
            <select name="selmonth">
            <%for i=1 to 12%>
                <%if cint(SelectedMonth)=i then%>
                    <option selected value="<%=i%>">
                    <%=monthname(i)%>
                    </option>
                <%else%>
                    <option value=< %=i%>>
                    <%=monthname(i)%>
                    </option>
                <%end if%>
            <%next%>
            </select>
            Year:
            <select name="selyear">
            <%for i=2000 to year(now)%>
                <option select value=<%=i%>>
                <%=i%>
                </option>
            <%next%>
            </select>
            <input type="Submit" value="Submit" name="btn_submit">
        </td>
    </tr> <!-- <-- missing! -->
</table>
</form>
<% 
dim dt 
dt = "1-" & monthname(Request.Form("sel_month")) & "-" & Request.Form("sel_year")
query = "select e.emp_id,e.emp_name,e.CompDepart,e.des_name,U.login_time,U.logoff_time,U.login_date " &_
  "from egv_emp_departments e " &_
  "inner join login_mast U on U.emp_id=e.emp_id " &_
  "where month(login_date)='" & request.form("selmonth") &_
  "' AND year(login_date)='" & request.form("selyear") &_
  "' AND e.emp_id='" & Session( "emp_id") & "'"
rs2.open query,con
%>
<table border="1" width="100%">
    <tr> <!-- added missing tr tag -->
        <td>emp_id</td>
        <td>emp_name</td>
        <td>CompDepart</td>
        <td>des_name</td>
        <%
        dim intDay 
        dim daysInMonth 
        dim strMonth 
        dim stryear 
        intDay=1 
        strMonth = request("selmonth") 
        stryear = request("selyear") 
        If strMonth <> "" Then 
            If stryear <> "" Then 
                Select Case strMonth 
                    Case 1,3,5,7,8, 10,12 daysInMonth = 31 
                    Case 4, 6, 9, 11 daysInMonth = 30 
                    Case 2
                        if stryear mod 4 = 0 Then 
                            If stryear mod 100 = 0 AND stryear mod 400 <> 0 Then 
                                daysInMonth = 28 
                            Else 
                                daysInMonth = 29 
                            End if 
                        Else 
                            daysInMonth=28 
                        End if 
                End Select 
                Do While intDay <= daysInMonth 
                    Response.Write "<td>" & intDay & "</td>"
                    intDay = intDay + 1 
                Loop 
            Else 
            End if 
        End if 

        for each x in rs2.fields ' <-- fixed typo "feilds"
            response.write "<th>" & x.name & "<th>"
        next
        %>
    </tr> <!-- added missing /tr tag -->
<%do until rs2.EOF %>
    <tr>
    <%for each x in rs2.Fields%>
        <td>
        <%Response.Write(x.value)%>
        </td>
    <% next %>
    <% rs2.MoveNext %>
    </tr>
<%loop 
rs2.close 
con.close %>
</table>

1 个答案:

答案 0 :(得分:0)

起初,我以为你想在Excel中使用所谓的“数据透视表”,但后来我注意到你正在过滤给一个员工。我仍然不清楚你的意思是“单日列”与“喜欢单列多列”,但我认为你的意思是:

Employee: Smith, John
ID: 123456
Month: March
—————————————————
Day  | In  |  Out
  1  |  8  |   5
  2  |  8  |   5
  3  |  -  |   -
  4  |  9  |   5
...  | ... |  ...
 30  |  9  |   4
 31  |  8  |   4
—————————————————

换句话说,您的问题是可能有几天没有数据,但您仍然想要打印这些行。处理它的最简单方法可能是一个临时表,其中包含数字1到31,然后您可以在外部联接中使用其余的查询。如果你不能这样做(例如你不允许创建临时表 - 不要笑,它会发生),那么你可以用代码来做。

在代码中执行此操作的最简单方法是将数据库中的值放入数组中,但不能使用GetRows。相反,将每一天的值放入数组的相应行中。

dim listdays(31,1)
...
Do Until rs2.EOF
    dt = Day(rs2("login_date"))
    listdays(dt,0) = rs2("login_time")
    listdays(dt,1) = rs2("logoff_time")
    rs2.Movenext
Loop

(我会在纯粹主义者完成任务时暂停。相信我,在包含最多31行的记录集上使用Movenext所造成的任何性能损失都将是如此微不足道,以至于不存在。)

然后你只需写出数组的内容。

Response.Write "<table><tr><th>Day</th><th>Login</th><th>Logoff</th></tr>"
For i = 1 to DaysInMonth
    Response.Write "<tr>"
    Response.Write "<th>" & i & "</th>"
    Response.Write "<td>" & listdays(i,0) & "</td>"
    Response.Write "<td>" & listdays(i,1) & "</td>"
    Response.Write "</tr>"
Next
Response.Write "</table>"