function treeNavToOptions($categories, $idToSelect, $level = -1) { $level++; $res = ''; foreach ($categories as $id => $cat) { $stringSpaces = ''; for ($i = 0; $i < $level; $i++) { $stringSpaces .= '-----------------'; } if ($idToSelect == $cat['idCreation']) { $selected = 'selected'; } else { $selected = ''; } $res .= '<option ' . $selected . ' value="' . $cat['idCreation'] . '">' . $stringSpaces . ' ' . htmlspecialchars($cat['title']) . '</option>'; if ($cat['child'] != null) { $res .= treeNavToOptions($cat['child'], $idToSelect, $level); } } return $res; }
<select class="txt" name="categorie"></select> </div> <?php foreach ($contentTypes as $contentType) { ?> <select style="display:none" id="categorie_<?php echo $contentType['id']; ?> "> <?php if ($contentType['id'] == 2) { ?> <option value="0">Aucune</option> <?php echo treeNavToOptions($treeNav, isset($infos['parent']) ? $infos['parent'] : 0); ?> <?php } else { ?> <?php foreach ($categories as $cat) { if ($cat['content_type'] == $contentType['id']) { if (isset($infos['categorie']) && $infos['categorie'] == $cat['id']) { $selected = 'selected'; } else { $selected = ''; } echo '<option ' . $selected . ' value="' . $cat['id'] . '">' . $cat['name'] . '</option>'; }