0

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

1
  • so you can iterate while you iterate... Commented Jun 8, 2009 at 1:24

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
$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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.