英文:
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 中,这很容易:
- 第一次循环将数据库结果放入多维数组中
- 第二次循环用于显示数组
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> </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 :
- first loop to put database results in a multidimensionnal array
- second loop to display the array
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>';
}
But in classic asp, how to have a multidimensionnal array ?
with array of array ? Dictionnary of arrays ? Dictionnary of Dictionnaries ?
答案1
得分: 0
以下是翻译好的部分:
<%
' 步骤 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'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
<%
' 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
%>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论