1

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...

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: http://blog.funeraldirectorslife.com/participants/

<?php
/*
Template Name: Participants
*/
?>
<?php
    function display_user($author, $month){
        $ID = $author->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->num_posts;
        //$months = array(January, February, March, April, May, June, July, August, September, October, November, December);
        //echo $sab_month;

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

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

        <div class="unit">
            <!--
            <div class="head">
                <div class="title"><h3><span>Blog Entries</span></h3></div>
            </div>
            //-->
            <div class="body clearfix">
                <ul class="styled_list">

                    <li class ="alpha"><h3 class="nomar"><a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span></a></h3></li>


                    <?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->get_results($sql);
                    $authors = $results;

                    $author_count = array();
                    $rows = $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author");
                    foreach ($rows as $row) {
                        $author_count[$row->post_author] = $row->count;
                    }
                    //combine queries
                    foreach( $authors as $author ) {
                        $author->num_posts = (isset($author_count[$author->ID])) ? $author_count[$author->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 --> same result
                    /*reset($months);
                    while (list($key, $month) = each($months)){
                        echo "<h2 class=\"nomar\">" .  $month . "</h2>" . "\n";

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



                    foreach($months as $month ) {
                        echo "<h2 class=\"nomar\">" .  $month . "</h2>" . "\n";

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


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

?>
5
  • could you also post the code for your display_user() function? It appears the problem may be there. Commented Jan 12, 2010 at 15:53
  • The display_user() function is at the top of the code Commented Jan 12, 2010 at 15:55
  • I'm a bit curious as to the where clause in your select statement. What does the "WHERE 1" do? Commented Jan 12, 2010 at 15:58
  • doesn't do anything in fact... looks like it was left over from a previous version. It has been removed to know read: SELECT * FROM wp_users ORDER BY ID Commented Jan 12, 2010 at 16:05
  • Ah, I thought that might be the case. Good to remove it, but I was kinda hoping that was the problem =P Anyway, see my answer below. I think it might help. Commented Jan 12, 2010 at 16:12

2 Answers 2

1

I'm not sure if it will work properly, but I have updated your code somewhat. Mainly I added the trim() function when comparing your $sab_month and $month but I also cleaned up your code a bit. You can embed variables inside of a double-quoted string in PHP, so you don't have to concatenate them :-)

<?php /* Template Name: Participants */ ?>

<?php
    function display_user($author, $month){
        $ID = $author->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->num_posts;
        //$months = array(January, February, March, April, May, June, July, August, September, October, November, December);
        //echo $sab_month;

        if (trim($sab_month) == trim($month)){
            echo "<li>";                                    
            echo "  <h4 class='nomar'><a href='http://blog.funeraldirectorslife.com/author/$usr_name'>$full_name($author_posts)</a></h4>";
            echo "  $sab_start - $sab_end<br />";
            echo "  $sab_descriptiom<br />";
            echo "  $sab_month</br>";
                echo "</li>\n";
        }
            /* Not necessary. The function will return when it hits the end
        else {
            return;
        }
            */
    }

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

        <div class="unit">
            <!--
            <div class="head">
                <div class="title"><h3><span>Blog Entries</span></h3></div>
            </div>
            //-->
            <div class="body clearfix">
                <ul class="styled_list">

                    <li class ="alpha"><h3 class="nomar"><a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span></a></h3></li>


                    <?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->get_results($sql);
                    $authors = $results;

                    $author_count = array();
                    $rows = $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author");
                    foreach ($rows as $row) {
                        $author_count[$row->post_author] = $row->count;
                    }
                    //combine queries
                    foreach( $authors as $author ) {
                        $author->num_posts = (isset($author_count[$author->ID])) ? $author_count[$author->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 --> same result
                    /*reset($months);
                    while (list($key, $month) = each($months)){
                        echo "<h2 class=\"nomar\">" .  $month . "</h2>" . "\n";

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



                    foreach($months as $month) {
                        echo "<h2 class='nomar'>$month</h2>\n";

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

                        //lists authors by ID - just making sure array is intact after each author loop
                        /*foreach($authors as $author) {
                            $ID = $author->ID;
                            $foo = get_cimyFieldValue("$ID", 'Sabbatical_Month');
                            echo $ID . $foo . "<br /> . "\n"";                  
                        }*/
                    }
                    ?>
                </ul>
            </div>      
        </div>
    </div>
</div>
<?php get_footer(); ?>
Sign up to request clarification or add additional context in comments.

Comments

0

appearantly this if fails after the first month if ($sab_month == $month) Maybe some typo, case differences perhaps? I'd try adding

echo "Sab month == ",$sab_month,"\n script month ==",$month;
Put it just before the if. good luck


I know I used comma's , thats just another way of using echo.

1 Comment

You can't see it unless you view the source, but apparently some of your months have extra space. Try trim($sab_month) == trim($month).

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.