bonjour,

voici la fonction que j'utilise :

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
function createSelectTree($data, $idParent, $indentSymbol, $level)
{
	$cat = array();
	foreach($data as $row){
		if($row->parent_id == $idParent){
			if($indentSymbol AND $level > 0){
				$symbols = array_fill(0, $level, $indentSymbol);
				$cat[$row->id] = implode('', $symbols) . $row->valeur;
			}else{
				$cat[$row->id] = $row->valeur;
			}
			$cat = $cat + createSelectTree($data, $row->id, $indentSymbol, $level++);
		}
	}
	return $cat;
}
Voici ma base de données Mysql :

Code sql : 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
--
-- Structure de la table `articles_familles`
--
 
CREATE TABLE IF NOT EXISTS `articles_familles` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `valeur` varchar(150) NOT NULL,
  `parent_id` bigint(20) NOT NULL,
  `id_structure_associe` int(11) NOT NULL DEFAULT '0',
  `date_ajout` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `ajoute_par` int(11) NOT NULL DEFAULT '0',
  `date_modif` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `modifie_par` int(11) NOT NULL DEFAULT '0',
  `date_archivage` date NOT NULL DEFAULT '0000-00-00',
  `motif_archivage` mediumint(9) NOT NULL DEFAULT '0',
  `archive_par` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `parent_id` (`parent_id`),
  KEY `id_structure_associe` (`id_structure_associe`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
 
--
-- Contenu de la table `articles_familles`
--
 
INSERT INTO `articles_familles` (`id`, `valeur`, `parent_id`, `id_structure_associe`, `date_ajout`, `ajoute_par`, `date_modif`, `modifie_par`, `date_archivage`, `motif_archivage`, `archive_par`) VALUES
(1, 'Groupe 1', 0, 2, '2013-11-14 16:49:26', 1, '2013-11-14 16:50:41', 1, '0000-00-00', 0, 0),
(2, 'Groupe 2', 0, 2, '2013-11-14 16:50:46', 1, '0000-00-00 00:00:00', 0, '0000-00-00', 0, 0),
(3, 'Famille 1', 1, 2, '2013-11-14 16:53:30', 1, '2013-11-14 16:53:38', 1, '0000-00-00', 0, 0),
(4, 'Famille 2', 1, 2, '2013-11-14 16:55:07', 1, '0000-00-00 00:00:00', 0, '0000-00-00', 0, 0),
(5, 'Famille 3', 1, 2, '2013-11-14 16:55:17', 1, '0000-00-00 00:00:00', 0, '0000-00-00', 0, 0),
(6, 'Xav', 1, 2, '0000-00-00 00:00:00', 0, '0000-00-00 00:00:00', 0, '0000-00-00', 0, 0),
(7, 'sssss', 2, 2, '0000-00-00 00:00:00', 0, '0000-00-00 00:00:00', 0, '0000-00-00', 0, 0);

cette fonction me sort la hiérarchie suivante :

Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
<select id="parent_id" name="parent_id">
<option value="0">Choisissez</option>
<option value="1">Groupe 1</option>
<option value="3">Famille 1</option>
<option value="4">-Famille 2</option>
<option value="5">--Famille 3</option>
<option value="6">---Famille 4 de groupe 1</option>
<option value="2">-Groupe 2</option>
<option value="7">-Famille 1 de groupe 2</option>
</select>

Comme vous le voyez, il manque un tiret à la première Famille 1 de Groupe 1, ensuite il ajoute trop de tirets et enfin alors que groupe 2 à un parent_id à 0, il me rajoute le symbole.

pouvez vous m'aider.

Merci.