【问题标题】:Foreach loop executes only first value with return, but returns all values with echoForeach 循环仅执行第一个值并返回,但返回所有值并回显
【发布时间】:2018-06-01 11:12:55
【问题描述】:

我正在尝试使用affiliatewp 创建会员排行榜。我能够获得我想要的每一个值,但是,在我使用的函数中,我使用了一个 foreach 循环来调用每个附属公司并显示当月的“销售额”。

function affiliate_leaderboard_function() {
global $wpdb;

$getallreferrals = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_referrals`");
$getallaffiliates = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_affiliates`");
$current_month = get_the_date("m");
$current_year = get_the_date("Y");
$current_date = get_the_date("Y-m-d");
$lastday = date('t',strtotime($current_date));

foreach ($getallaffiliates as $theaffiliate) {
    $user_id = get_userdata( $theaffiliate->user_id );
    $userfirstname = $user_id->first_name;
    $userlastname = $user_id->last_name;
    $totalreferrals = $theaffiliate->referrals;
    $affiliate_id = $theaffiliate->affiliate_id;
    $getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59' ORDER BY `wp_affiliate_wp_referrals`.`referral_id` ASC");//Get all referrals by affiliate id
    $closerstring = $userfirstname." | This Month's Sales: ".count($getaffreferrals)."<br>";
    if(!empty($getaffreferrals)){
        echo $closerstring;
    }
}
}
add_shortcode('affiliate_leaderboard' , 'affiliate_leaderboard_function');

所以当我放置短代码并使用“echo $closerstring”时,它会吐出所有正确的数据,用户的名字后跟他们当月的销售额。但是内容顶部的简码输出。

当我将它切换到“return $closerstring”时,它只返回一个附属而不是 foreach 循环中的所有附属。我不知道如何让它像 echo 函数那样显示所有值,但我需要它出现在正确的位置......

【问题讨论】:

    标签: php wordpress foreach affiliate


    【解决方案1】:

    在forloop中,如果你写return,那么循环在第一次运行时退出。相反,使用变量 将您的代码更改为

    $return = '';
    foreach ($getallaffiliates as $theaffiliate) {
    $user_id = get_userdata( $theaffiliate->user_id );
    $userfirstname = $user_id->first_name;
    $userlastname = $user_id->last_name;
    $totalreferrals = $theaffiliate->referrals;
    $affiliate_id = $theaffiliate->affiliate_id;
    $getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59' ORDER BY `wp_affiliate_wp_referrals`.`referral_id` ASC");//Get all referrals by affiliate id
    $closerstring = $userfirstname." | This Month's Sales: ".count($getaffreferrals)."<br>";
    if(!empty($getaffreferrals)){
        $return_array.= $closerstring;
    }
    }
    
    return $return_array;
    

    【讨论】:

      【解决方案2】:

      return 中断函数的执行。

      您需要将输出保存在字符串中并在最后返回,如下所示:

      [...]
          $output= '';
          foreach ($getallaffiliates as $theaffiliate) {
              [...]
              if(!empty($getaffreferrals)){
                  $output .= $closerstring;
              }
          }
          return $output;
      }
      

      【讨论】:

        【解决方案3】:

        在每个循环之后构建你的字符串,而不是 echo 然后 return 它在最后:

        $result = ""; // Create an empty string
        
        foreach ($getallaffiliates as $theaffiliate) {
          $user_id = get_userdata( $theaffiliate->user_id );
          $userfirstname = $user_id->first_name;
          $userlastname = $user_id->last_name;
          $totalreferrals = $theaffiliate->referrals;
          $affiliate_id = $theaffiliate->affiliate_id;
          $getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59' ORDER BY `wp_affiliate_wp_referrals`.`referral_id` ASC");//Get all referrals by affiliate id
          $closerstring = $userfirstname." | This Month's Sales: ".count($getaffreferrals)."<br>";
          if(!empty($getaffreferrals)){
            $result .= $closerstring; // Add the data
          }
        }
        
        return $result; // you return the full string
        

        【讨论】:

          【解决方案4】:

          追加字符串并返回。

              function affiliate_leaderboard_function() {
              global $wpdb;
          
                  $getallreferrals = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_referrals`");
                  $getallaffiliates = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_affiliates`");
                  $current_month = get_the_date("m");
                  $current_year = get_the_date("Y");
                  $current_date = get_the_date("Y-m-d");
                  $lastday = date('t',strtotime($current_date));
          
                  $returnstring='';
                  foreach ($ge1tallaffiliates as $theaffiliate) {
                    $user_id = get_userdata( $theaffiliate->user_id );
                    $userfirstname = $user_id->first_name;
                    $userlastname = $user_id->last_name;
                    $totalreferrals = $theaffiliate->referrals;
                    $affiliate_id = $theaffiliate->affiliate_id;
                    $getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59' ORDER BY `wp_affiliate_wp_referrals`.`referral_id` ASC");//Get all referrals by affiliate id
                 $closerstring = $userfirstname." | This Month's Sales: ".count($getaffreferrals)."<br>";
                  if(!empty($getaffreferrals)){
                      $returnstring .=$closerstring;
                     }
                 }
                     return $returnstring;
              }
              add_shortcode('affiliate_leaderboard' , 'affiliate_leaderboard_function');
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-16
            • 2013-01-13
            • 2016-01-09
            • 2019-06-04
            • 2019-03-22
            • 2018-07-11
            • 2023-03-10
            相关资源
            最近更新 更多