PHP: Nested Foreach loop problems
<p>I am attempting to display a list of users based on a month value. I want to display January then all the users associated with it etc...</p>

<p>What i have now is working correctly for the first months then does nothing after. I have comments in the code and a live version of this code is running at: <a href="http://blog.funeraldirectorslife.com/participants/" rel="nofollow">http://blog.funeraldirectorslife.com/participants/</a></p>

<pre><code>&lt;?php
/*
Template Name: Participants
*/
?&gt;
&lt;?php
    function display_user($author, $month){
        $ID = $author-&gt;ID;
        $full_name = get_the_author_meta('display_name', $ID);
        $usr_name = get_the_author_meta('user_nicename', $ID);
        $sab_descriptiom = get_cimyFieldValue("$ID", 'Sabbatical_desc');
        $sab_start = get_cimyFieldValue("$ID", 'Sabbatical_Start');
        $sab_end = get_cimyFieldValue("$ID", 'Sabbatical_End');
        $sab_month = get_cimyFieldValue("$ID", 'Sabbatical_Month');
        $author_posts = $author-&gt;num_posts;
        //$months = array(January, February, March, April, May, June, July, August, September, October, November, December);
        //echo $sab_month;

        if ($sab_month == $month){
            echo "&lt;li&gt;";                                    
            echo "&lt;h4 class=\"nomar\"&gt;&lt;a href=\"http://blog.funeraldirectorslife.com/author/" 
                    . $usr_name                                  
                    ."\"&gt;"
                    .$full_name 
                    ."(" 
                    . $author_posts
                    . ")" 
                    . "&lt;/a&gt;" 
                    . "&lt;/h4&gt;";
            echo    $sab_start 
                    . " - " 
                    . $sab_end 
                    . "&lt;br /&gt;";
            echo    $sab_descriptiom 
                    . "&lt;br /&gt;"
                    . $sab_month . "&lt;/br&gt;" . "&lt;/li&gt;" . "\n";
        }
        else {
            return;
        }
    }

?&gt;
&lt;?php get_header(); ?&gt;
&lt;div id="bd" class="grid_12 alpha omega clearfix"&gt;
    &lt;?php get_sidebar(); ?&gt;
    &lt;div id="main" class="grid_9 omega"&gt;
        &lt;?php include(TEMPLATEPATH . '/banner.php'); ?&gt;

        &lt;div class="unit"&gt;
            &lt;!--
            &lt;div class="head"&gt;
                &lt;div class="title"&gt;&lt;h3&gt;&lt;span&gt;Blog Entries&lt;/span&gt;&lt;/h3&gt;&lt;/div&gt;
            &lt;/div&gt;
            //--&gt;
            &lt;div class="body clearfix"&gt;
                &lt;ul class="styled_list"&gt;

                    &lt;li class ="alpha"&gt;&lt;h3 class="nomar"&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;span&gt;&lt;?php the_title(); ?&gt;&lt;/span&gt;&lt;/a&gt;&lt;/h3&gt;&lt;/li&gt;


                    &lt;?php                       
                    //grab authors ids, post count, and extended info from database
                    //fill $authors array with data
                    $sql = "SELECT * FROM `wp_users` WHERE 1 ORDER BY ID";  
                    $results = $wpdb-&gt;get_results($sql);
                    $authors = $results;

                    $author_count = array();
                    $rows = $wpdb-&gt;get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb-&gt;posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author");
                    foreach ($rows as $row) {
                        $author_count[$row-&gt;post_author] = $row-&gt;count;
                    }
                    //combine queries
                    foreach( $authors as $author ) {
                        $author-&gt;num_posts = (isset($author_count[$author-&gt;ID])) ? $author_count[$author-&gt;ID] : 0;
                    }

                    //month array for comparing with sabbatical month 
                    $months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December');


                    //tried old method for iterating arrays --&gt; same result
                    /*reset($months);
                    while (list($key, $month) = each($months)){
                        echo "&lt;h2 class=\"nomar\"&gt;" .  $month . "&lt;/h2&gt;" . "\n";

                        foreach($authors as $author) {
                            display_user($author, $month);  
                            //echo $author-&gt;ID;
                        }
                    }*/



                    foreach($months as $month ) {
                        echo "&lt;h2 class=\"nomar\"&gt;" .  $month . "&lt;/h2&gt;" . "\n";

                        foreach($authors as $author) {
                            display_user($author, $month);  
                            //echo $author-&gt;ID;
                        }


                        //lists authors by ID - just making sure array is intact after each author loop
                        /*foreach($authors as $author) {
                            $ID = $author-&gt;ID;
                            $foo = get_cimyFieldValue("$ID", 'Sabbatical_Month');
                            echo $ID . $foo . "&lt;br /&gt; . "\n"";                  
                        }*/
                    }
                    ?&gt;
                &lt;/ul&gt;
            &lt;/div&gt;      
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;?php get_footer(); 

?&gt;
</code></pre>
