File "tree_view06.php"

Full Path: /home/analogde/www/php/FormData/uploads/2024_PHP_02_01_2025/tree_view06.php
File size: 2.86 KB
MIME-type: text/x-php
Charset: utf-8

<?php
// Initialiser le tableau
$filesArray = [
    "fichiers" => []
];

// Fonction pour ajouter un fichier avec ses enfants
function addFile($parent, $children) {
    global $filesArray; // Accéder au tableau global

    // Si le parent n'existe pas encore, on l'initialise
    if (!isset($filesArray['fichiers'][$parent])) {
        $filesArray['fichiers'][$parent] = [];
    }

    // Ajouter les enfants au parent
    foreach ($children as $child) {
        $filesArray['fichiers'][$parent][] = $child;
    }
}

// Déclaration des fichiers et enfants
addFile("fichier1", ["fichier2", "fichier3"]);
addFile("toto1", ["toto2", "toto3", "toto4"]);

// Pour afficher le tableau pour vérification
echo '<pre>';
print_r($filesArray);
echo '</pre>';

// Convertir en JSON pour utilisation ultérieure
$jsonData = json_encode($filesArray);
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tree Viewer</title>
    <style>
        ul {
            list-style-type: none;
            padding-left: 20px;
        }
        .toggle {
            cursor: pointer;
            color: blue;
            text-decoration: underline;
        }
    </style>
</head>
<body>

<h1>Arborescence des fichiers</h1>
<ul id="fileTree"></ul>

<script>
    // Utiliser le JSON généré par PHP
    const filesData = <?php echo $jsonData; ?>;

    function createTree(files) {
        const ul = document.createElement('ul');

        for (const [parent, children] of Object.entries(files.fichiers)) {
            const li = document.createElement('li');
            const span = document.createElement('span');
            span.classList.add('toggle');
            span.textContent = parent;

            li.appendChild(span);

            if (children.length > 0) {
                const childUl = document.createElement('ul');
                children.forEach(child => {
                    const childLi = document.createElement('li');
                    childLi.textContent = child;
                    childUl.appendChild(childLi);
                });
                li.appendChild(childUl);
            }

            ul.appendChild(li);
        }

        return ul;
    }

    const fileTree = createTree(filesData);
    document.getElementById('fileTree').appendChild(fileTree);

    document.querySelectorAll('.toggle').forEach(item => {
        item.addEventListener('click', event => {
            const nextUl = item.nextElementSibling;
            if (nextUl) {
                nextUl.style.display = nextUl.style.display === 'none' || nextUl.style.display === '' ? 'block' : 'none';
            }
        });
    });

    // Initialiser l'affichage des sous-listes
    /*
    document.querySelectorAll('#fileTree ul').forEach(ul => {
        ul.style.display = 'none'; // Par défaut, caché
    });*/
</script>

</body>
</html>