IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP Discussion :

Tableaux multidimensionnels pour stocker, puis afficher des données groupées par champ


Sujet :

ASP

  1. #1
    Membre actif
    Homme Profil pro
    développeur
    Inscrit en
    Octobre 2004
    Messages
    479
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : développeur
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Octobre 2004
    Messages : 479
    Points : 281
    Points
    281
    Par défaut Tableaux multidimensionnels pour stocker, puis afficher des données groupées par champ
    Bonjour,

    Je dois maintenir une ancienne application.
    J'ai ces données en base :

    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

    Je tente de les afficher sous cette forme :
    • COMPANY 1
      • CATEGORY 1:
        • BRAND X
          BRAND Y
          BRAND Z

        CATEGORY 2
        • BRAND A
          BRAND B
          BRAND C

        CATEGORY 3
        • BRAND A
          BRAND X


    En php, c'est simple avec un tableau de tableaux :

    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
      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>';
      }

    En asp, comment faire ?
    Comment avoir un tableau multidimensionnel ?
    Un tableau de tableaux ?
    Un dictionnaire de tableaux ?
    Un dictionnaire de dictionnaires ?

  2. #2
    Membre actif
    Homme Profil pro
    développeur
    Inscrit en
    Octobre 2004
    Messages
    479
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : développeur
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Octobre 2004
    Messages : 479
    Points : 281
    Points
    281
    Par défaut
    Quelques pistes trouvées de ci de là, mais pas simple à réaliser.
    Je me suis résolu à demander à ChatGPT.

    La question :
    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
    La réponse :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    <%
    ' 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
    %>

    Et ça fonctionne très bien

    Le principe était de savoir comment créer un dictionnaire de dictionnaires.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 14/04/2020, 16h57
  2. Réponses: 3
    Dernier message: 18/08/2015, 20h00
  3. Réponses: 13
    Dernier message: 18/06/2011, 14h01
  4. [DOM] PHP et XML pour stocker et afficher des données
    Par sasaas dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 22/02/2008, 18h20
  5. [MySQL] rejoindre deux requêtes pour afficher des données
    Par schats dans le forum PHP & Base de données
    Réponses: 8
    Dernier message: 26/12/2007, 14h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo