Foreach循环仅使用return执行第一个值,但返回带有echo的所有值

时间:2018-06-01 11:12:56

标签: php wordpress foreach affiliate

我正在尝试使用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函数那样的所有值,但我需要它出现在正确的位置......

4 个答案:

答案 0 :(得分:0)

返回中断函数的执行。

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

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

答案 1 :(得分:0)

在每次循环后构建字符串,而不是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

答案 2 :(得分:0)

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

RUNPATH

答案 3 :(得分:0)

追加字符串并返回。

    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');