classic ASP – 用于存储和显示按共同字段分组的多行的多维数组

huangapple go评论60阅读模式
英文:

classic ASP - Multidimensionnal array for storing and displaying multiple rows grouped by common fields

问题

我有数据库中的这些数据

公司   | 类别   | 品牌    
--------------------------------
公司 1 | 类别 2 | 品牌 A  
公司 1 | 类别 2 | 品牌 B  
公司 1 | 类别 2 | 品牌 C  
公司 1 | 类别 1 | 品牌 X  
公司 1 | 类别 1 | 品牌 Y  
公司 1 | 类别 1 | 品牌 Z  
公司 1 | 类别 3 | 品牌 A  
公司 1 | 类别 3 | 品牌 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 中,如何获得多维数组?

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

英文:

I've these datas in database

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  

I would like to display them in this way :

COMPANY 1

  • CATEGORY 1:

BRAND X

BRAND Y

BRAND Z

  • CATEGORY 2

BRAND A

BRAND B

BRAND C

  • CATEGORY 3

BRAND A

BRAND X

In php, it's easy :

  1. first loop to put database results in a multidimensionnal array
  2. second loop to display the array
  while ($row = mysql_fetch_assoc($result)) {
    $companies[$row[&#39;company&#39;]][$row[&#39;category&#39;]][] = $row[&#39;brand&#39;];
  }

  foreach ($companies AS $company =&gt; $categories) {
    echo &#39;&lt;h2&gt;&#39;. htmlentities($company, ENT_COMPAT, &#39;UTF-8&#39;) .&#39;&lt;/h2&gt;&#39;;
    echo &#39;&lt;ul&gt;&#39;;
    foreach ($categories AS $category =&gt; $brands) {
      echo &#39;&lt;li&gt;&#39;. htmlentities($category, ENT_COMPAT, &#39;UTF-8&#39;);
      foreach ($brands AS $brand) {
        echo &#39;&lt;br&gt;&lt;em&gt;&#39;. htmlentities($brand, ENT_COMPAT, &#39;UTF-8&#39;) .&#39;&lt;/em&gt;&#39;;
      }
      echo &#39;&lt;br&gt;&amp;nbsp;&lt;/li&gt;&#39;;
    }
    echo &#39;&lt;/ul&gt;&#39;;
  }

But in classic asp, how to have a multidimensionnal array ?

with array of array ? Dictionnary of arrays ? Dictionnary of Dictionnaries ?

答案1

得分: 0

以下是翻译好的部分:

&lt;%
' 步骤 1:从数据库检索数据(假设您已连接到数据库并有一个名为“rs”的记录集)

' 创建一个嵌套数据结构来保存分层数据
Dim data
Set data = CreateObject("Scripting.Dictionary")

' 循环遍历记录集并将数据组织成嵌套结构
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")

    ' 检查数据字典中是否存在 niveau1 键
    If Not data.Exists(niveau1) Then
        data.Add niveau1, CreateObject("Scripting.Dictionary")
    End If

    ' 检查 niveau1 字典中是否存在 niveau2 键
    If Not data(niveau1).Exists(niveau2) Then
        data(niveau1).Add niveau2, CreateObject("Scripting.Dictionary")
    End If

    ' 检查 niveau2 字典中是否存在 niveau3 键
    If Not data(niveau1)(niveau2).Exists(niveau3) Then
        data(niveau1)(niveau2).Add niveau3, CreateObject("Scripting.Dictionary")
    End If

    ' 检查 niveau3 字典中是否存在 niveau4 键
    If Not data(niveau1)(niveau2)(niveau3).Exists(niveau4) Then
        data(niveau1)(niveau2)(niveau3).Add niveau4, CreateObject("Scripting.Dictionary")
    End If

    ' 将 niveau5 添加到 niveau4 字典中
    data(niveau1)(niveau2)(niveau3)(niveau4).Add niveau5, Nothing

    rs.MoveNext
Loop

' 步骤 3:从嵌套数据结构生成 HTML 列表
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

' 从顶层(Niveau1)开始生成列表
GenerateList data, 0
%>

请注意,上述代码是 Classic ASP 代码,用于从数据库中检索数据并将其以分层列表的形式显示在 HTML 中。

英文:

After several hours to test some code, i ask to ChatGPT

classic ASP - get data from database and display them in a 5 depth list
I&#39;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

The answer is OK

    &lt;%
&#39; Step 1: Retrieve data from the database (assuming you have already connected to the database and have a recordset called &quot;rs&quot;)

&#39; Create a nested data structure to hold the hierarchical data
Dim data
Set data = CreateObject(&quot;Scripting.Dictionary&quot;)

&#39; 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(&quot;Niveau1&quot;)
    niveau2 = rs(&quot;Niveau2&quot;)
    niveau3 = rs(&quot;Niveau3&quot;)
    niveau4 = rs(&quot;Niveau4&quot;)
    niveau5 = rs(&quot;Niveau5&quot;)

    &#39; Check if the niveau1 key exists in the data dictionary
    If Not data.Exists(niveau1) Then
        data.Add niveau1, CreateObject(&quot;Scripting.Dictionary&quot;)
    End If

    &#39; Check if the niveau2 key exists in the niveau1 dictionary
    If Not data(niveau1).Exists(niveau2) Then
        data(niveau1).Add niveau2, CreateObject(&quot;Scripting.Dictionary&quot;)
    End If

    &#39; Check if the niveau3 key exists in the niveau2 dictionary
    If Not data(niveau1)(niveau2).Exists(niveau3) Then
        data(niveau1)(niveau2).Add niveau3, CreateObject(&quot;Scripting.Dictionary&quot;)
    End If

    &#39; 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(&quot;Scripting.Dictionary&quot;)
    End If

    &#39; Add niveau5 to the niveau4 dictionary
    data(niveau1)(niveau2)(niveau3)(niveau4).Add niveau5, Nothing

    rs.MoveNext
Loop

&#39; Step 3: Generate the HTML list from the nested data structure
Sub GenerateList(data, depth)
    If depth &gt; 0 Then
        Response.Write(&quot;&lt;ul&gt;&quot;)
    End If

    For Each key In data.Keys
        Response.Write(&quot;&lt;li&gt;&quot; &amp; key &amp; &quot;&lt;/li&gt;&quot;)
        If IsObject(data(key)) Then
            GenerateList data(key), depth + 1
        End If
    Next

    If depth &gt; 0 Then
        Response.Write(&quot;&lt;/ul&gt;&quot;)
    End If
End Sub

&#39; Start generating the list from the top level (Niveau1)
GenerateList data, 0
%&gt;

huangapple
  • 本文由 发表于 2023年7月27日 22:13:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定