I am confused about multi-dimentional array.
In the following db, php and html, I don't understand the usage of
foreach ($navlist as $key => $list){
foreach ($list as $topkey => $toplist){..
and
foreach ($toplist['children'] as $subkey => $subname){...
And also this code is confusing for me. Is ['children'] php?
$data[0][$row->parentid]['children'][$row->id] = $row->name;
I will appreciate if you can explain this navgation.php
Thanks in advance.
I have the followings in db.
INSERT INTO `categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`)
VALUES (1, 'shoes', 'Shoes for boys and girls.', '', 'active', 7);
INSERT INTO `categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`)
VALUES (2, 'shirts', 'Shirts and blouses!', '', 'active', 7);
[...]
INSERT INTO `categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`)
VALUES (9, 'test', 'testing', 'Testing!!!!', 'inactive', 0);
Model has the following php
function getCategoriesNav(){
$data = array();
$this->db->select('id,name,parentid');
$this->db->where('status', 'active');
$this->db->orderby('parentid','asc');
$this->db->orderby('name','asc');
$this->db->groupby('parentid,id');
$Q = $this->db->get('categories');
if ($Q->num_rows() > 0){
foreach ($Q->result() as $row){
if ($row->parentid > 0){
$data[0][$row->parentid]['children'][$row->id] = $row->name;
}else{
$data[0][$row->id]['name'] = $row->name;
}
}
}
$Q->free_result();
return $data;
}
And controller/navigation.php
if (count($navlist)){
echo "<ul>";
foreach ($navlist as $key => $list){
foreach ($list as $topkey => $toplist){
echo "<li class='cat'>";
echo anchor("welcome/cat/$topkey",$toplist['name']);
echo "</li>\n";
if (count($toplist['children'])){
foreach ($toplist['children'] as $subkey => $subname){
echo "\n<li class='subcat'>";
echo anchor("welcome/cat/$subkey",$subname);
echo "</li>";
}
}
}
}
echo "</ul>\n";
}
This will produce the following html
<ul>
<li class='cat'>
<a href="http://127.0.0.1/codeigniter_shopping/welcome/cat/7" title="clothes">
clothes
</a>
</li>
<li>
...
</li>
<li class='subcat'>
<a href="http://127.0.0.1/codeigniter_shopping/welcome/cat/5" title="toys">
toys
</a>
</li>
</ul>