tags:

views:

161

answers:

2

I'm trying to display the associated forums inside a category during a loop in /categories/index

   Array
(
    [0] => Array
        (
            [Category] => Array
                (
                    [id] => 1
                    [name] => General
                    [status] => 1
                    [order] => 1
                )

            [Forum] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [category_id] => 1
                            [name] => Lounge
                            [description] => Go and lounge around
                            [status] => 1
                            [order] => 1
                            [total_posts] => 1
                            [total_threads] => 1
                            [created] => 2009-06-04 19:13:24
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [category_id] => 1
                            [name] => Test111
                            [description] => Test111
                            [status] => 1
                            [order] => 1
                            [total_posts] => 1
                            [total_threads] => 1
                            [created] => 2009-06-04 19:16:26
                        )

                )

        )

However to get the forum's to display I need to set the array value ([0]) and obviously this isnt going to work during a foreach loop, how do I loop the categories then loop the forums inside of categories

+3  A: 

This should do it if the variable holding everything is $categories:

print '<ul>';
foreach($categories as $category) {
   print '<li>' . $category['Category']['name'];
   if($category['Forum']) {
      print '<ul>';
      foreach($category['Forum'] as $forum) {
          print '<li>' . $forum['name'] . '</li>';
      }
      print '</ul>';
   }
   print '</li>';
}
print '</ul>';

The HTML structure is just an example you can change it around to be on a table or whatever.

Paolo Bergantino
A: 
$result_set=array(....);//Your main array
foreach($result_set as $category){
  print_my_category_header($category['category']);
  print_category_forums($category['Forum'];
}


function print_my_category_header(array $category){
   //do what you need to do
}

function print_category_forums(array $forums){
   foreach($forums as $forum){
       echo_single_forum($forum);
   }
}

function echo_single_forum(array $forum){
      //echo fields in the way you want to
}
Itay Moav