经典 ASP - 用于存储和显示按公共字段分组的多行的多维数组

问题描述 投票:0回答:1

我的数据库中有这些数据

COMPANY   | CATEGORY   | BRAND    
--------------------------------
Company 1 | Category 2 | Brand A  
Company 1 | Category 2 | Brand B  
Company 1 | Category 2 | Brand C  
Company 1 | Category 1 | Brand X  
Company 1 | Category 1 | Brand Y  
Company 1 | Category 1 | Brand Z  
Company 1 | Category 3 | Brand A  
Company 1 | Category 3 | Brand X  

我想以这种方式展示它们:

公司1

  • 类别 1:

品牌X

Y品牌

Z品牌

  • 类别 2

品牌A

品牌B

品牌C

  • 类别 3

品牌A

品牌X

在 php 中,这很简单 :

  1. 将数据库结果放入多维数组的第一个循环
  2. 第二个循环显示数组
  while ($row = mysql_fetch_assoc($result)) {
    $companies[$row['company']][$row['category']][] = $row['brand'];
  }

  foreach ($companies AS $company => $categories) {
    echo '<h2>'. htmlentities($company, ENT_COMPAT, 'UTF-8') .'</h2>';
    echo '<ul>';
    foreach ($categories AS $category => $brands) {
      echo '<li>'. htmlentities($category, ENT_COMPAT, 'UTF-8');
      foreach ($brands AS $brand) {
        echo '<br><em>'. htmlentities($brand, ENT_COMPAT, 'UTF-8') .'</em>';
      }
      echo '<br>&nbsp;</li>';
    }
    echo '</ul>';
  }

但是在经典的asp中,如何拥有多维数组?

使用数组的数组?数组字典?字典中的字典?

multidimensional-array display
1个回答
0
投票

经过几个小时测试一些代码后,我要求 ChatGPT

classic ASP - get data from database and display them in a 5 depth list
I've these column datas in database

Niveau1 | Niveau2| Niveau3| Niveau4| Niveau5

I would like to display them in list group by Niveau5, inside list group by Niveau4, inside list group by Niveau3, inside list group by Niveau2, inside list group by Niveau1

答案是好的

    <%
' Step 1: Retrieve data from the database (assuming you have already connected to the database and have a recordset called "rs")

' Create a nested data structure to hold the hierarchical data
Dim data
Set data = CreateObject("Scripting.Dictionary")

' Loop through the recordset and organize the data into the nested structure
Do While Not rs.EOF
    Dim niveau1, niveau2, niveau3, niveau4, niveau5
    niveau1 = rs("Niveau1")
    niveau2 = rs("Niveau2")
    niveau3 = rs("Niveau3")
    niveau4 = rs("Niveau4")
    niveau5 = rs("Niveau5")

    ' Check if the niveau1 key exists in the data dictionary
    If Not data.Exists(niveau1) Then
        data.Add niveau1, CreateObject("Scripting.Dictionary")
    End If

    ' Check if the niveau2 key exists in the niveau1 dictionary
    If Not data(niveau1).Exists(niveau2) Then
        data(niveau1).Add niveau2, CreateObject("Scripting.Dictionary")
    End If

    ' Check if the niveau3 key exists in the niveau2 dictionary
    If Not data(niveau1)(niveau2).Exists(niveau3) Then
        data(niveau1)(niveau2).Add niveau3, CreateObject("Scripting.Dictionary")
    End If

    ' Check if the niveau4 key exists in the niveau3 dictionary
    If Not data(niveau1)(niveau2)(niveau3).Exists(niveau4) Then
        data(niveau1)(niveau2)(niveau3).Add niveau4, CreateObject("Scripting.Dictionary")
    End If

    ' Add niveau5 to the niveau4 dictionary
    data(niveau1)(niveau2)(niveau3)(niveau4).Add niveau5, Nothing

    rs.MoveNext
Loop

' Step 3: Generate the HTML list from the nested data structure
Sub GenerateList(data, depth)
    If depth > 0 Then
        Response.Write("<ul>")
    End If

    For Each key In data.Keys
        Response.Write("<li>" & key & "</li>")
        If IsObject(data(key)) Then
            GenerateList data(key), depth + 1
        End If
    Next

    If depth > 0 Then
        Response.Write("</ul>")
    End If
End Sub

' Start generating the list from the top level (Niveau1)
GenerateList data, 0
%>
© www.soinside.com 2019 - 2024. All rights reserved.