Sorry - really bad title but couldn't figure how else to put it!
I am creating an array of a folder structure like this:
Each entry in the table has three fields:
| parent id | route | label |
|-----------|-------|------------|
| 1 | | account |
| 2 | 1 | section 1 |
| 3 | 1 | section 2 |
| 4 | 1/2 | s1 options |
| 5 | 1/3 | s2 options |
| 6 | 1/2/4 | settings |
| 7 | 1/3/5 | language |
This would look like this:
account /
account / section 1 /
account / section 2 /
account / section 1 / s1 options /
account / section 2 / s2 options /
account / section 1 / s1 options / settings
account / section 2 / s2 options / language
To create this the route
is parsed using the parent id
and its label
. All straightforward so far.
As I'm sure you'll appreciate, I want to list it as:
account /
account / section 1 /
account / section 1 / s1 options /
account / section 1 / s1 options / settings
account / section 2 /
account / section 2 / s2 options /
account / section 2 / s2 options / language
This is where I'm having problems! I've tried different sort options both in the sql query and various array options but I'm getting stuck.
I have a page where a user can create new folders by selecting the parent folder first and then adding in the new folder name, so I don't have control over how the folder structure is built or in what order.
At the moment I'm trying to avoid anything too long and complex because I'm sure there must be a way of doing it simply...I just can't find it.
Suggestions/solutions valued! :)
SQL -
$q1 = $dbp->prepare("SELECT * FROM `structure`");
$q1->execute();
while ($d1 = $q1->fetch()) {
$structure_parent[] = $d1['parent'];
$structure_route[] = $d1['route'];
$structure_label[] = $d1['label'];
$structure[$d1['parent']] = $d1['label'];
}
SELECT -
<select id="parent" name="parent">
<?php
$p = 0;
while($p < count($structure_parent)) {
// build route map
$route = explode("/", $structure_route[$p]);
$s = 0;
while($s < count($route)) {
$route_mapping[] = $structure[$route[$s]];
$s++;
}
if($structure_route[$p] > "") $routemap = implode(" / ", $route_mapping)." / ";
else $routemap = "";
unset($route_mapping);
?>
<option value="<?php echo $structure_parent[$p]; ?>"><?php echo " / ".$routemap.$structure_label[$p]; ?></option>
<?php
$p++;
}
?>
</select>