2010-01-12 231 views
1

我試圖根據月份值顯示用戶列表。我想顯示一月,然後所有與它相關的用戶等...PHP:嵌套的Foreach循環問題

我現在有什麼是正常工作的第一個月,然後什麼都沒做。我在代碼和該代碼的一個真人版的評論在運行: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(); 

?> 
+0

可能還張貼您的display_user()函數的代碼?看來問題可能在那裏。 – GSto 2010-01-12 15:53:34

+0

display_user()函數位於代碼頂部 – alphaexe 2010-01-12 15:55:52

+0

我對你的select語句中的where子句有點好奇。 「WHERE 1」做什麼? – 2010-01-12 15:58:57

回答

1

我不確定它是否能正常工作,但我已經更新了您的代碼。主要是在比較你的$sab_month$month時增加了trim()函數,但我也清理了一下你的代碼。您可以嵌入變量PHP中的雙引號字符串內,所以你不必將它們串聯:-)

<?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(); ?> 
+0

這工作。非常感謝:) – alphaexe 2010-01-12 16:20:37

+0

不客氣:-) – 2010-01-12 16:22:22

0

appearantly這一點,如果第一個月if ($sab_month == $month) 也許有些錯字,情況差異也許以後失敗了呢?我會嘗試加入

echo "Sab month == ",$sab_month,"\n script month ==",$month;
把它放在if之前。祝你好運


我知道我用逗號的,這只是另一種使用回聲的方式。

+0

除非您查看源代碼,否則無法看到它,但顯然有些月份有額外的空間。試一下'trim($ sab_month)== trim($ month)'。 – 2010-01-12 16:05:37