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

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

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

问题

我有数据库中的这些数据

  1. 公司 | 类别 | 品牌
  2. --------------------------------
  3. 公司 1 | 类别 2 | 品牌 A
  4. 公司 1 | 类别 2 | 品牌 B
  5. 公司 1 | 类别 2 | 品牌 C
  6. 公司 1 | 类别 1 | 品牌 X
  7. 公司 1 | 类别 1 | 品牌 Y
  8. 公司 1 | 类别 1 | 品牌 Z
  9. 公司 1 | 类别 3 | 品牌 A
  10. 公司 1 | 类别 3 | 品牌 X

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

公司 1

  • 类别 1:

品牌 X

品牌 Y

品牌 Z

  • 类别 2

品牌 A

品牌 B

品牌 C

  • 类别 3

品牌 A

品牌 X

在 PHP 中,这很容易:

  1. 第一次循环将数据库结果放入多维数组中
  2. 第二次循环用于显示数组
  1. while ($row = mysql_fetch_assoc($result)) {
  2. $companies[$row['company']][$row['category']][] = $row['brand'];
  3. }
  4. foreach ($companies AS $company => $categories) {
  5. echo '<h2>' . htmlentities($company, ENT_COMPAT, 'UTF-8') .'</h2>';
  6. echo '<ul>';
  7. foreach ($categories AS $category => $brands) {
  8. echo '<li>' . htmlentities($category, ENT_COMPAT, 'UTF-8');
  9. foreach ($brands AS $brand) {
  10. echo '<br><em>' . htmlentities($brand, ENT_COMPAT, 'UTF-8') .'</em>';
  11. }
  12. echo '<br>&nbsp;</li>';
  13. }
  14. echo '</ul>';
  15. }

但在经典 ASP 中,如何获得多维数组?

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

英文:

I've these datas in database

  1. COMPANY | CATEGORY | BRAND
  2. --------------------------------
  3. Company 1 | Category 2 | Brand A
  4. Company 1 | Category 2 | Brand B
  5. Company 1 | Category 2 | Brand C
  6. Company 1 | Category 1 | Brand X
  7. Company 1 | Category 1 | Brand Y
  8. Company 1 | Category 1 | Brand Z
  9. Company 1 | Category 3 | Brand A
  10. 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
  1. while ($row = mysql_fetch_assoc($result)) {
  2. $companies[$row[&#39;company&#39;]][$row[&#39;category&#39;]][] = $row[&#39;brand&#39;];
  3. }
  4. foreach ($companies AS $company =&gt; $categories) {
  5. echo &#39;&lt;h2&gt;&#39;. htmlentities($company, ENT_COMPAT, &#39;UTF-8&#39;) .&#39;&lt;/h2&gt;&#39;;
  6. echo &#39;&lt;ul&gt;&#39;;
  7. foreach ($categories AS $category =&gt; $brands) {
  8. echo &#39;&lt;li&gt;&#39;. htmlentities($category, ENT_COMPAT, &#39;UTF-8&#39;);
  9. foreach ($brands AS $brand) {
  10. echo &#39;&lt;br&gt;&lt;em&gt;&#39;. htmlentities($brand, ENT_COMPAT, &#39;UTF-8&#39;) .&#39;&lt;/em&gt;&#39;;
  11. }
  12. echo &#39;&lt;br&gt;&amp;nbsp;&lt;/li&gt;&#39;;
  13. }
  14. echo &#39;&lt;/ul&gt;&#39;;
  15. }

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

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

答案1

得分: 0

以下是翻译好的部分:

  1. &lt;%
  2. ' 步骤 1:从数据库检索数据(假设您已连接到数据库并有一个名为“rs”的记录集)
  3. ' 创建一个嵌套数据结构来保存分层数据
  4. Dim data
  5. Set data = CreateObject("Scripting.Dictionary")
  6. ' 循环遍历记录集并将数据组织成嵌套结构
  7. Do While Not rs.EOF
  8. Dim niveau1, niveau2, niveau3, niveau4, niveau5
  9. niveau1 = rs("Niveau1")
  10. niveau2 = rs("Niveau2")
  11. niveau3 = rs("Niveau3")
  12. niveau4 = rs("Niveau4")
  13. niveau5 = rs("Niveau5")
  14. ' 检查数据字典中是否存在 niveau1
  15. If Not data.Exists(niveau1) Then
  16. data.Add niveau1, CreateObject("Scripting.Dictionary")
  17. End If
  18. ' 检查 niveau1 字典中是否存在 niveau2 键
  19. If Not data(niveau1).Exists(niveau2) Then
  20. data(niveau1).Add niveau2, CreateObject("Scripting.Dictionary")
  21. End If
  22. ' 检查 niveau2 字典中是否存在 niveau3
  23. If Not data(niveau1)(niveau2).Exists(niveau3) Then
  24. data(niveau1)(niveau2).Add niveau3, CreateObject("Scripting.Dictionary")
  25. End If
  26. ' 检查 niveau3 字典中是否存在 niveau4 键
  27. If Not data(niveau1)(niveau2)(niveau3).Exists(niveau4) Then
  28. data(niveau1)(niveau2)(niveau3).Add niveau4, CreateObject("Scripting.Dictionary")
  29. End If
  30. ' niveau5 添加到 niveau4 字典中
  31. data(niveau1)(niveau2)(niveau3)(niveau4).Add niveau5, Nothing
  32. rs.MoveNext
  33. Loop
  34. ' 步骤 3:从嵌套数据结构生成 HTML 列表
  35. Sub GenerateList(data, depth)
  36. If depth > 0 Then
  37. Response.Write("<ul>")
  38. End If
  39. For Each key In data.Keys
  40. Response.Write("<li>" & key & "</li>")
  41. If IsObject(data(key)) Then
  42. GenerateList data(key), depth + 1
  43. End If
  44. Next
  45. If depth > 0 Then
  46. Response.Write("</ul>")
  47. End If
  48. End Sub
  49. ' 从顶层(Niveau1)开始生成列表
  50. GenerateList data, 0
  51. %>

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

英文:

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

  1. classic ASP - get data from database and display them in a 5 depth list
  2. I&#39;ve these column datas in database
  3. Niveau1 | Niveau2| Niveau3| Niveau4| Niveau5
  4. 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

  1. &lt;%
  2. &#39; Step 1: Retrieve data from the database (assuming you have already connected to the database and have a recordset called &quot;rs&quot;)
  3. &#39; Create a nested data structure to hold the hierarchical data
  4. Dim data
  5. Set data = CreateObject(&quot;Scripting.Dictionary&quot;)
  6. &#39; Loop through the recordset and organize the data into the nested structure
  7. Do While Not rs.EOF
  8. Dim niveau1, niveau2, niveau3, niveau4, niveau5
  9. niveau1 = rs(&quot;Niveau1&quot;)
  10. niveau2 = rs(&quot;Niveau2&quot;)
  11. niveau3 = rs(&quot;Niveau3&quot;)
  12. niveau4 = rs(&quot;Niveau4&quot;)
  13. niveau5 = rs(&quot;Niveau5&quot;)
  14. &#39; Check if the niveau1 key exists in the data dictionary
  15. If Not data.Exists(niveau1) Then
  16. data.Add niveau1, CreateObject(&quot;Scripting.Dictionary&quot;)
  17. End If
  18. &#39; Check if the niveau2 key exists in the niveau1 dictionary
  19. If Not data(niveau1).Exists(niveau2) Then
  20. data(niveau1).Add niveau2, CreateObject(&quot;Scripting.Dictionary&quot;)
  21. End If
  22. &#39; Check if the niveau3 key exists in the niveau2 dictionary
  23. If Not data(niveau1)(niveau2).Exists(niveau3) Then
  24. data(niveau1)(niveau2).Add niveau3, CreateObject(&quot;Scripting.Dictionary&quot;)
  25. End If
  26. &#39; Check if the niveau4 key exists in the niveau3 dictionary
  27. If Not data(niveau1)(niveau2)(niveau3).Exists(niveau4) Then
  28. data(niveau1)(niveau2)(niveau3).Add niveau4, CreateObject(&quot;Scripting.Dictionary&quot;)
  29. End If
  30. &#39; Add niveau5 to the niveau4 dictionary
  31. data(niveau1)(niveau2)(niveau3)(niveau4).Add niveau5, Nothing
  32. rs.MoveNext
  33. Loop
  34. &#39; Step 3: Generate the HTML list from the nested data structure
  35. Sub GenerateList(data, depth)
  36. If depth &gt; 0 Then
  37. Response.Write(&quot;&lt;ul&gt;&quot;)
  38. End If
  39. For Each key In data.Keys
  40. Response.Write(&quot;&lt;li&gt;&quot; &amp; key &amp; &quot;&lt;/li&gt;&quot;)
  41. If IsObject(data(key)) Then
  42. GenerateList data(key), depth + 1
  43. End If
  44. Next
  45. If depth &gt; 0 Then
  46. Response.Write(&quot;&lt;/ul&gt;&quot;)
  47. End If
  48. End Sub
  49. &#39; Start generating the list from the top level (Niveau1)
  50. GenerateList data, 0
  51. %&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:

确定