views:

115

answers:

2

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>
+3  A: 

The basic construct is this:

foreach ($navlist as $key => $list)

This means iterate through the array $navlist, and for each time in $navlist, with key $key and value $list, execute the body of the foreach loop.

A simpler example might help.

$numbers = array();
$numbers[0] = "Zero";
$numbers[1] = "One";
$numbers[2] = "Two";

foreach($numbers as $number => $text) {
    echo $number." is written ".$text."\n";
}

The output of that (unless I've screwed up somewhere), is:

0 is written Zero
1 is written One
2 is written Two

The code you've given then just has multiple nested loops, so $list in the outermost for-loop is itself an associative array, which is then looped through, and the values stored in that associative array are also arrays.

You might find reading the foreach documentation instructive.

Dominic Rodger
A: 

please explain what is the meaning of
foreach ($toplist['children'] as $subkey => $subname) and where the children come from , im confused

Mehdy