Search code examples
phpwordpressforeachaffiliate

Foreach loop executes only first value with return, but returns all values with echo


I'm trying to create an affiliate leaderboard using affiliatewp. I'm able to get every value that I want, however, in the function I'm using a foreach loop that calls every affiliate and displays the "sales" for the month.

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

So when I put the shortcode in place, and with using "echo $closerstring" it spits out all of the correct data, user's first name followed by how many sales they've had in the current month. BUT the shortcode outputs at the top of the content.

When I switch it to "return $closerstring" it only returns one affiliate instead of all affiliates in the foreach loop. I have no clue how to get it to show all values like the echo function does, but I need it to appear in the proper location...


Solution

  • In forloop, if you write return, then the loop exits at the first run it self. instead, using a variable change your code to

    $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;