Hey John, thanks - worked like a charm!
Now, I do have another question. I thought I had my array creation set up properly but it looks like I do not - my nav is only going two levels deep. Here's the function I've got currently(this is in CodeIgniter, but it shouldn't make a difference):
Code: Select all
protected function _get_nav_array() {
// SELECT slug, id, parent_id, title FROM nav WHERE display = 1
$sql = $this->CI->db->select('slug, id, parent_id, title')->where('display',1)->get('nav');
// Turn the result into an array
$arr = $sql->result_array();
// Put a placeholder value in the [0] index of the array because
// everything was offset by 1
array_unshift($arr,'placeholder');
// Loop through the array
foreach($arr as $item) {
// parent_id = 0 indicates that it is a top-level nav element
// and nothing further is required
if($item['parent_id'] != 0) {
// Example: $array[1]['children'][0] = $item
$arr[$item['parent_id']]['children'][] = $item;
// Pop that off the end of the array so we don't have duplicate values
array_pop($arr);
}
}
// Shift the placeholder index back off of the top
array_shift($arr);
// Just a setter.
$this->_set_nav_array($arr);
}
So the problem, which I believe lies in my use of array_pop(), is that any element that has a parent_id of another subnav element gets destroyed at some point during the process. Here's an example of what my table looks like:
Code: Select all
id parent_id title slug display
1 0 About Us about 1
2 0 Demo Page demo-page 1
3 1 Subnav1 sub 1
4 1 Subnav2 subnav 1
5 2 Other Subnav subnavv 1
6 3 SubSubNav subsub 1
Everything is displaying as expected except for the final entry on the table which has parent_id set to 3. Any suggestions?