3

SQL SERVERデータベースに呼び出さmytableれたテーブルがあり、ASPを使用してHTMLテーブルに表示したいと考えています。実行すると、HTMLテーブルがレンダリングされますSELECT * FROM mytableここに画像の説明を入力してください

そのデータベースから、それをグループ化し、HTMLテーブルを次のように変更します 。ここに画像の説明を入力してください 重複する値を削除し、テーブルを。とマージしますrowspanrowspanグループ化されたHTMLテーブルを作成するようにループする方法は?

これが私がこれまでに作ったものです:

<%
Set oConnection = Server.CreateObject("ADODB.Connection")
oConnection.Open Dsn

response.write "<table border='1' width='100%' cellpadding='5' cellspacing='0'>" &_
                    "<tr>" &_
                        "<td align='center' valign='middle' bgcolor='#CCCCCC'><strong>field1</strong></td>" &_
                        "<td align='center' valign='middle' bgcolor='#CCCCCC'><strong>field2</strong></td>" &_      
                        "<td align='center' valign='middle' bgcolor='#CCCCCC'><strong>field3</strong></td>" &_
                    "</tr>"

strselect = "select * from mytable"

set qdata = oConnection.execute(strselect)

If qdata.EOF then
    Response.write("NO DATA")   
Else    
    Do While Not qdata.EOF
    response.write  "<tr> " &_
                        "<td>" & trim(qdata("field1")) & "</td>"&_
                        "<td>" & trim(qdata("field2")) & "</td>"&_
                        "<td>" & trim(qdata("field3")) & "</td> " &_                            
                    "</tr>" 
    qdata.MoveNext
    Loop    
End If      

response.write "</table>"
oConnection.Close
Set oConnection = Nothing
%>
4

1 に答える 1

2

これはGetRows()、ADO RecordSet のメソッドを利用する良い機会と思われます。 GetRows()レコードセットを多次元配列に変換します。これは、値を比較するためにウォークスルーするのがかなり簡単です。この場合、重要な機能です。

次のコードは、テスト データで動作します。

' Assuming the connection and recordset have already been created previous to this point
Response.Write "<table border='1' width='100%' cellpadding='5' cellspacing='0'>"
' Uncomment below when using the recordset
' Response.Write GetHeaderHtml(qdata)

Response.Write "<tbody>"

' Uncomment below when using the recordset
' Dim rsArray : rsArray = qdata.GetRows
' qdata.Close
' oConnection.Close
' Set qdata = Nothing
' Set oConnection = Nothing

' FOR TESTING ONLY:
Dim rsArray(2, 9)
rsArray(0, 0) = "X1"
rsArray(0, 1) = "X1"
rsArray(0, 2) = "X1"
rsArray(0, 3) = "X1"
rsArray(0, 4) = "X1"
rsArray(0, 5) = "X2"
rsArray(0, 6) = "X2"
rsArray(0, 7) = "X2"
rsArray(0, 8) = "X2"
rsArray(0, 9) = "X2"

rsArray(1, 0) = "A"
rsArray(1, 1) = "A"
rsArray(1, 2) = "A"
rsArray(1, 3) = "B"
rsArray(1, 4) = "B"
rsArray(1, 5) = "A"
rsArray(1, 6) = "A"
rsArray(1, 7) = "B"
rsArray(1, 8) = "C"
rsArray(1, 9) = "C"

rsArray(2, 0) = "12"
rsArray(2, 1) = "332"
rsArray(2, 2) = "32"
rsArray(2, 3) = "14"
rsArray(2, 4) = "10"
rsArray(2, 5) = "155"
rsArray(2, 6) = "23"
rsArray(2, 7) = "25"
rsArray(2, 8) = "32"
rsArray(2, 9) = "38"
' END TESTING DATA

Dim rowHtml, occurances, row, col
For row = LBound(rsArray, 2) To UBound(rsArray, 2)    ' the second dimension is row
  rowHtml = "<tr>"
  For col = LBound(rsArray, 1) To UBound(rsArray, 1)  ' the first dimension is column
    If row > LBound(rsArray, 2) Then
      ' previous rows written, only write out the cell if it is different than the one above.
      If rsArray(col, row) <> rsArray(col, row - 1) Then
        occurances = CountColumnOccurances(col, row, rsArray)
        ' you could probably get away writing "rowspan='1'", but I'll test for it and omit if 1
        If occurances > 1 Then
          rowHtml = rowHtml & "<td rowspan='" & CountColumnOccurances(col, row, rsArray) & "'>"
        Else
          rowHtml = rowHtml & "<td>"
        End If
        rowHtml = rowHtml & Server.HTMLEncode(rsArray(col, row))
        rowHtml = rowHtml & "</td>"
      End If
    Else
      occurances = CountColumnOccurances(col, row, rsArray)
      ' you could probably get away writing "rowspan='1'", but I'll test for it and omit if 1
      If occurances > 1 Then
        rowHtml = rowHtml & "<td rowspan='" & CountColumnOccurances(col, row, rsArray) & "'>"
      Else
        rowHtml = rowHtml & "<td>"
      End If
      rowHtml = rowHtml & Server.HTMLEncode(rsArray(col, row))
      rowHtml = rowHtml & "</td>"
    End If
  Next ' col
  rowHtml = rowHtml & "</tr>" & vbCrlf

  Response.Write rowHtml
Next ' row
Response.Write "</tbody>"
Response.Write "</table>"

Function CountColumnOccurances(curCol, curRow, arr)
  Dim occurances  : occurances = 1                  ' how many repeats
  Dim examinedRow : examinedRow = curRow + 1        ' the row we're comparing to
  Dim curValue    : curValue = arr(curCol, curRow)  ' the value were using to compare

  Do While examinedRow <= UBound(arr, 2)            ' the second dimension is row
    If arr(curCol, examinedRow) = curValue Then     ' the next row has the same value
      occurances = occurances + 1
    Else                                            ' the next row is different
      Exit Do
    End If
    examinedRow = examinedRow + 1
  Loop

  CountColumnOccurances = occurances
End Function

Function GetHeaderHtml(rsData)
  Dim strHeaders : strHeaders = "<thead><tr>"
  Dim objField

  For Each objField In rsData.Fields
    strHeaders = strHeaders & "<th align='center' valign='middle' bgcolor='#CCCCCC'><strong>" & objField.Name & "</strong></th>"
  Next
  strHeaders = strHeaders & "</tr></thead>"

  GetHeaderHtml = strHeaders
End Function
于 2012-08-27T15:54:53.247 に答える