2

我有一段代码按字母顺序从网页上的数据库ACROSS 3 列中写入数据。

例子:

a 结果 b 结果 c 结果
d 结果 e 结果 f 结果
g 结果 h 结果 i 结果

我需要按字母顺序显示它 DOWN the columns,如下所示:

结果 d 结果 g 结果
b 结果 e 结果 h 结果
c 结果 f 结果 i 结果

请记住,我有大约 100 个数据结果,它将显示第一列中的前 1/3 降序,然后开始一个新列并继续,将其分成 3 个相等的部分。

我现在对行进行排序的代码是:

<% 
GL="<table width="+Z+"100%"+Z+"border=0 cellpadding=3 celspacing=3>"
sql="select * from guideDef order by guideDesc;"
rs.open sql,adoCon
colCount=0
do while not rs.eof
  colCount=(colCount+1) mod 3
  if colCount=1 then GL=GL+"<tr>" 
  GL=GL+"<td valign=middle id=menu1 width="+Z+"33%"+Z+">"+E 
  GL=GL+"<a href="+Z+"shop.asp?guide="+rs("guide")+"&city=Plantation"+Z+">"+E 
  GL=GL+rs("guideDesc")+"</a></td>" 
  if colCount=0 then GL=GL+"</tr>" 
  GL=GL+E
  rs.moveNext
loop
rs.close
if colCount=1 then GL=GL+"<td> </td><td> </td></tr>"+E
if colCount=2 then GL=GL+"<td> </td></tr>"+E
GL=GL+"</table>"
response.write GL
%>

提前感谢您的帮助。我不写代码,所以我尝试了几个小时来改变它但没有成功。

4

7 回答 7

5

也许更好的解决方案是保留 SQL 并在应用程序代码中处理它,而不是作为查询的结果。

于 2009-02-18T18:12:59.397 回答
3

我相信这段代码会解决你的问题:

<%
Set rs     = Server.CreateObject("ADODB.RecordSet")
Set adoCon = Server.CreateObject("ADODB.Connection")

adoCon.Open "your connection string here"

Const COLUMN_COUNT    = 3

Const adOpenStatic    = 3

sql = "SELECT guide, guideDesc FROM guideDef ORDER BY guideDesc;" 
rs.Open sql, adoCon, adOpenStatic

CellsRemain = rs.RecordCount Mod COLUMN_COUNT
RowCount    = (rs.RecordCount - CellsRemain) / COLUMN_COUNT

Response.Write "<div>Rendering " & rs.RecordCount & " records to a " & _
               COLUMN_COUNT & " x " & RowCount & " table with " & _
               CellsRemain & " stand-alone cells.</div>"

Response.Write "<table width=""100%"" border=""0"" cellpadding=""3"" celspacing=""3"">" & vbCrLf

done = 0
cell = 0
While done < rs.RecordCount
  Response.Write "<tr>"  & vbCrLf
  While cell < COLUMN_COUNT And done < rs.RecordCount
    cell      = cell + 1
    done      = done + 1
    guide     = "" & rs("guide")
    guideDesc = "" & rs("guideDesc")
    url       = "shop.asp?guide=" + Server.UrlEncode(guide) + "&city=Plantation"
    Response.Write "<td>"
    Response.Write "<a href=""" & Server.HtmlEncode(url) & """>"
    Response.Write Server.HtmlEncode(guideDesc)
    Response.Write "</td>"  & vbCrLf
    If cell < COLUMN_COUNT Then rs.Move RowCount
  Wend
  If done < rs.RecordCount Then 
    rs.Move -1 * ((COLUMN_COUNT - 1) * RowCount - 1)
    cell = 0
  Else
    While cell < COLUMN_COUNT
      Response.Write "<td>&nbsp;</td>" & vbCrLf
      cell = cell + 1
    Wend
  End If
  Response.Write "</tr>" & vbCrLf
Wend

Response.Write "</table>" & vbCrLf
%>

这会按照您想要的方式呈现您的表格:

AEH
BFI
CGJ
D

您可以使用COLUMN_COUNT常量来控制将创建多少列。该算法灵活地适应该数字。

代码的作用基本上是这样的:

  1. 打开一个静态 RecordSet 对象,这样我们就可以在其中自由跳转
  2. 计算我们需要显示所有记录的行数和列数
  3. <tr>
  4. 分步跳下 RecordSet RowCount,绘制<td>s 直到<tr>
  5. 跳回我们在第 4 步开始的记录之后的记录
  6. </tr>
  7. 如果仍有记录,请转到步骤 3
  8. 渲染尽可能多的空单元格,以使表格格式正确
  9. 完毕。
于 2009-02-18T18:24:36.297 回答
1

查看使用 PIVOT AND UNPIVOT 命令。

于 2009-02-18T18:02:14.773 回答
1

忽略有关使用交叉表的所有答案,他们没有阅读您的问题。

我要做的是将您的结果作为一个巨大的表格并将它们分成三个不同的集合,然后遍历每一行从集合一中插入项目一,然后是集合二,然后是集合三,等等,直到您用尽所有三个集合。

另一种选择是编写代码,该代码将向下一列直到使用三分之一的结果,然后移动到下一列,但考虑到 HTML 的排序方式,编写起来会有点困难。

于 2009-02-18T18:22:56.347 回答
1

您可以将结果分为 3 部分(如果您知道行数)。在单独的 div 元素中将它们打印为 3 个单独的表格。然后,您可以使用 CSS 将 div 元素彼此相邻浮动。

如果这听起来像你想要做的,(因为你说你不写代码)让我知道你是否需要帮助。

于 2009-02-18T18:33:22.500 回答
0

这也称为交叉表查询。您需要使用带有一些聚合函数的 case 语句,请参见此处

http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=25

于 2009-02-18T18:19:29.773 回答
0

我会做这样的事情(在 C# 中):

const int columns = 3;
string[] cells = GetCells(); // load your sql into this
string ret = "<table>";
int numRows = (cells.Length + columns - 1) / columns; // round up
for (int y = 0; y < numRows; y++)
{
    ret += "<tr>";
    for (int x = 0; x < columns; x++)
    {
        int elem = x*numRows + y;
        if (elem < cells.Length)
            ret += "<td>" + cells[elem] + "</td>";
        else
            ret += "<td>&nbsp;</td>";
    }
    ret += "</tr>";
}
ret += "</table>";

和 GetCells() 看起来像:

string[] GetCells()
{
    string sql = "SELECT guide, guideDesc FROM guideDef ORDER BY guideDesc";
    rs.Open(sql, adoCon, adOpenStatic);
    string[] ret = new string[rs.RecordCount]
    for (int i=0; i<rs.RecordCount; i++)
    {
        ret[i] = "<a href=...></a>";
    }
    return ret;
}
于 2009-02-18T20:28:19.837 回答