[eluser]TheFuzzy0ne[/eluser]
I think everyone seems to have a different perception of what it is I'm actually trying to achieve, so I will tell you exactly what I my goal is. In my forum, I require a select box which shows the forum structure, but uses indentation so you can see the structure clearly. So it would look something like this (using CodeIgniter's forum as an example forum structure):
Code:
Forum Home
The CodeIgniter Lounge
Introduce Yourself!
The Lounge
CodeIgniter Development Forums
CodeIgniter Discussion
Code and Application Development
Ignited Code
Job Board
Feature Requests
Bug Reports
Wiki Article Discussions
I need this in an array so I can use it with form_dropdown, so that might look something like this:
Code:
Array
(
[1] => Forum Home
[54] => The CodeIgniter Lounge
[59] => Introduce Yourself!
[55] => The Lounge
[48] => CodeIgniter Development Forums
[49] => CodeIgniter Discussion
[53] => Code and Application Development
[58] => Ignited Code
[65] => Job Board
[52] => Feature Requests
[51] => Bug Reports
[71] => Wiki Article Discussions
)
The array key is the ID of the forum, and will be used as the value for the select option.
As I use MPTT for my forum structure, I need to convert the tree itself into an array, which requires a bit of recursion. Here's the tree:
Code:
Array
(
[id] => 1
[title] => Forum Home
[children] => Array
(
[0] => Array
(
[id] => 54
[title] => The CodeIgniter Lounge
[children] => Array
(
[0] => Array
(
[id] => 59
[title] => Introduce Yourself!
)
[1] => Array
(
[id] => 55
[title] => The Lounge
)
)
)
[1] => Array
(
[id] => 48
[title] => CodeIgniter Development Forums
[children] => Array
(
[0] => Array
(
[id] => 49
[title] => CodeIgniter Discussion
)
[1] => Array
(
[id] => 53
[title] => Code and Application Development
)
[2] => Array
(
[id] => 58
[title] => Ignited Code
)
[3] => Array
(
[id] => 65
[title] => Job Board
)
[4] => Array
(
[id] => 52
[title] => Feature Requests
)
[5] => Array
(
[id] => 51
[title] => Bug Reports
)
[6] => Array
(
[id] => 71
[title] => Wiki Article Discussions
)
)
)
)
)
Here's the function:
Code:
function forum_dropdown_options($tree, $indent_level=0)
{
$ret = array();
if (isset($tree['id']) && isset($tree['title']))
{
$ret[$tree['id']] = str_repeat(' ', $indent_level).$tree['title'];
if (isset($tree['children']))
{
foreach ($tree['children'] as $forum)
{
$ret += forum_dropdown_options($forum, $indent_level +1);
}
}
}
return $ret;
}
And here's the resulting array:
Code:
Array
(
[1] => Forum Home
[54] => The CodeIgniter Lounge
[59] => Introduce Yourself!
[55] => The Lounge
[48] => CodeIgniter Development Forums
[49] => CodeIgniter Discussion
[53] => Code and Application Development
[58] => Ignited Code
[65] => Job Board
[52] => Feature Requests
[51] => Bug Reports
[71] => Wiki Article Discussions
)
Thanks, everyone.