[eluser]Lazos[/eluser]
What I am trying to do is to list a menu. At the Top will be parent menu and below the child menu.
Now if I am not mistaken when the key name of the array is childs it will do the iteration for all the variables inside the {childs}{/childs} but what about the parents that they have key names 0,1,2? Can I do something to change them so I can do the {top_titles}{/top_titles} iteration?
I want to parse a multi-dimensional array using the parser class like this:
Code:
{top_titles}
<li><a href="<?=base_url()?>index.php/{link}">{title}</a>
<ul>
{childs}
<li><a href="<?=base_url()?>index.php/{link}">{title}</a></li>
{/childs}
</ul>
</li>
{/top_titles}
The Controller looks like this:
Code:
<?php
class Home extends Controller {
function _get_child_array($iStart)
{
// Init
$aCategories = array();
// Call database
$aCategorieOverview = $this->Header->get_overview($iStart);
return $aCategorieOverview;
}
function _get_category_array($iStart)
{
// Init
$aCategories = array();
// Call database
$aCategorieOverview = $this->Header->get_overview($iStart);
// For all categories found
foreach ( $aCategorieOverview as $sKey=>$sValue ) {
// create new category item and fill it up
$aCategory = array();
$aCategory['id'] = $aCategorieOverview[$sKey]->id;
$aCategory['title'] = $aCategorieOverview[$sKey]->title;
// Get childs if there are any (recusive) and add them to this level
$aChildCategories = Home::_get_child_array($aCategory['id']);
if ( !empty($aChildCategories) ) {
$aCategory['childs'] = $aChildCategories;
}
// Add category to the categories array
$aCategories[] = $aCategory;
}
// Return the results
return $aCategories;
}
function index()
{
$this->load->model('Header');
$array = $this->_get_category_array(-1);
$this->parser->parse('admin/header.tpl', $array);
}
}
The Model looks like this:
Code:
<?php
class Header extends Model {
function Header()
{
// Call the Model constructor
parent::Model();
}
function get_overview($iStart)
{
$this->load->database();
$query = $this->db->get_where('my_admin_cp_menu', array('pid' => $iStart));
return $query->result();
}
}
?>
If I try to print the array and see what is inside them I get the following result:
Code:
Array
(
[0] => Array
(
[id] => 1
[title] => CMS
[childs] => Array
(
[0] => stdClass Object
(
[id] => 2
[title] => Home
[pid] => 1
[link] => admin/home
[hierarchy] => 001.001
[id_hierarchy] => 1.2
)
[1] => stdClass Object
(
[id] => 3
[title] => View Site
[pid] => 1
[link] =>
[hierarchy] => 001.002
[id_hierarchy] => 1.3
)
[2] => stdClass Object
(
[id] => 23
[title] => Log Out
[pid] => 1
[link] => admin/logout
[hierarchy] => 001.003
[id_hierarchy] => 1.23
)
)
)
[1] => Array
(
[id] => 4
[title] => Content
[childs] => Array
(
[0] => stdClass Object
(
[id] => 5
[title] => Pages
[pid] => 4
[link] => admin/listcontent
[hierarchy] => 002.001
[id_hierarchy] => 4.5
)
[1] => stdClass Object
(
[id] => 6
[title] => Image Manager
[pid] => 4
[link] => admin/imagemanager
[hierarchy] => 002.002
[id_hierarchy] => 4.6
)
[2] => stdClass Object
(
[id] => 7
[title] => File Manager
[pid] => 4
[link] => admin/filemanager
[hierarchy] => 002.003
[id_hierarchy] => 4.7
)
[3] => stdClass Object
(
[id] => 8
[title] => News
[pid] => 4
[link] => admin/news
[hierarchy] => 002.004
[id_hierarchy] => 4.8
)
)
)