I've been trying to create a multi-level ordered list in PHP for some time now. I've tried and have been been searching and everything I've seen has a different data layout than what I have to work with. Most examples pull the data from a database and build the array based on separate columns and, in come cases, columns with depth or parental relationships information. My data is in a path to an SSRS report and can vary from 2 to 3 (or more in the future) levels so I'm not sure how to build the array to work with the function in this question.
For instance, one example is from another post here - generating-category-tree-as-html-unordered-list, but the difference in all the examples and what I have is with the nature of the data itself.
Here's the full example I'm working with:
Example array and function
<?php
$nested = Array
(
1 => Array ('id' => 1, 'parent' => 0, 'title' => 'Page 1'),
2 => Array ('id' => 2, 'parent' => 0, 'title' => 'Page 2'),
3 => Array ('id' => 3, 'parent' => 0, 'title' => 'Page 3'),
4 => Array ('id' => 4, 'parent' => 0, 'title' => 'Page 4' ),
5 => Array ('id' => 5, 'parent' => 0, 'title' => 'Page 5'),
6 => Array ('id' => 6, 'parent' => 1, 'title' => 'Page 1-1'),
7 => Array ('id' => 7, 'parent' => 1, 'title' => 'Page 1-2'),
8 => Array ('id' => 8, 'parent' => 1, 'title' => 'Page 1-3'),
9 => Array ('id' => 9, 'parent' => 2, 'title' => 'Page 2-1'),
10 => Array ('id' => 10, 'parent' => 2, 'title' => 'Page 2-2'),
11 => Array ('id' => 11, 'parent' => 2, 'title' => 'Page 2-3'),
12 => Array ('id' => 12, 'parent' => 3, 'title' => 'Page 3-1'),
13 => Array ('id' => 13, 'parent' => 3, 'title' => 'Page 3-2'),
14 => Array ('id' => 14, 'parent' => 4, 'title' => 'Page 4-1'),
15 => Array ('id' => 15, 'parent' => 6, 'title' => 'Page 1-1-1'),
16 => Array ('id' => 16, 'parent' => 6, 'title' => 'Page 1-1-2'),
17 => Array ('id' => 17, 'parent' => 6, 'title' => 'Page 1-1-3'),
18 => Array ('id' => 18, 'parent' => 7, 'title' => 'Page 1-2-1'),
19 => Array ('id' => 19, 'parent' => 7, 'title' => 'Page 1-2-2'),
20 => Array ('id' => 20, 'parent' => 7, 'title' => 'Page 1-2-3'),
21 => Array ('id' => 21, 'parent' => 9, 'title' => 'Page 2-1-1'),
22 => Array ('id' => 22, 'parent' => 9, 'title' => 'Page 2-1-2'),
23 => Array('id' => 23, 'parent' => 12, 'title' => 'Page 2-3-3')
);
function recursive($parent, $array) {
$has_children = false;
foreach($array as $key => $value) {
if ($value['parent'] == $parent) {
if ($has_children === false && $parent) {
$has_children = true;
echo '<ul>' ."\n";
}
echo '<li>' . "\n";
echo '<a href="/page.php?id=' . $value['id'] . '">' . $value['title'] . '</a>' . " \n";
echo "\n";
recursive($key, $array);
echo "</li>\n";
}
}
if ($has_children === true && $parent) echo "</ul>\n";
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<ul><?php echo recursive(0, $nested); ?></ul>
</body>
</html>
Here is an example of the data out of the SSRS report server. Since the data is so dense, it won't fit in a code window and make any sense so I'm going to have to stack the two columns. The report name is identical as the last element in the path, so it's somewhat redundant anyway.
Database Data
PATH
/Production/Analyst/POS Sales Excel Data Export
/Production/Analyst/STATS Membership Tracking - RAW
/Production/Information Technologies/IT Dashboard DUMMY
/Production/Information Technologies/IT Department
/Production/Membership/MDash
/Production/Membership/Membership Dashboard
/Production/Membership/Membership Reports/Membership Grid
/Production/Membership/Membership Reports/Membership Grid by SU
/Production/Membership/Membership Reports/Membership Pie Charts
/Production/Membership/Membership Reports/Membership Sum Drill Down
/Production/Membership/Membership Reports/POS Sales By Date and Location
/Production/Membership/Stats/STATS Council Participation
/Production/Membership/Stats/STATS Council SU Summary Adults
/Production/Membership/Stats/STATS Council Summary Adults
/Production/Membership/Stats/STATS Council Troop Summary Adults
/Production/Membership/Stats/STATS Lifetime Stats
/Production/Membership/Stats/STATS Membership Tracking
/Production/Online Training (LMS)/LMS User ID Lookup
/Production/Online Training (LMS)/Manager Training 2014
/Production/Shop/POS Reports/POS Sales By Date and Location
/Production/Shop/Shop Dashboard DUMMY
/Production/SLT Dashboard/OLD Dashboard Membership_Overview
NAME
POS Sales Excel Data Export
STATS Membership Tracking - RAW
IT Dashboard DUMMY
IT Department
MDash
Membership Dashboard
Membership Grid
Membership Grid by SU
Membership Pie Charts
Membership Sum Drill Down
POS Sales By Date and Location
STATS Council Participation
STATS Council SU Summary Adults
STATS Council Summary Adults
STATS Council Troop Summary Adults
STATS Lifetime Stats
STATS Membership Tracking
LMS User ID Lookup
Manager Training 2014
POS Sales By Date and Location
Shop Dashboard DUMMY
OLD Dashboard Membership_Overview
That data would need need to go in a format like that below to work with the function. The ID is irrelevant to my purposes since the link to the report is the same as the report name so I would have to change the function to suit. That said, I need to take the path above and turn it into an array like that below.
Array format needed
$nested2 = Array
(
1 => Array ('parent' => 0, 'title' => 'Analyst'),
2 => Array ('parent' => 0, 'title' => 'Membership'),
3 => Array ('parent' => 0, 'title' => 'SLT Dashboard'),
4 => Array ('parent' => '1', 'title' => 'POS Sales Excel Data Export'),
5 => Array ('parent' => '1', 'title' => 'STATS Membership Tracking - RAW'),
6 => Array ('parent' => '2', 'title' => 'Membership Dashboard'),
7 => Array ('parent' => '6', 'title' => 'Membership Grid'),
8 => Array ('parent' => '6', 'title' => 'Membership Grid by Service Unit'),
);
So, my question is - how to convert the data I'm working with into an array like this? Or, would it be better to use something different to create the list? I know I can iterated through the list and probably need to compare each element to the report name to know where to stop, who the parent is, etc, but sort of get lost after that.
As always, many thanks in advance.