Php Foreach循环只执行第一个返回值,但返回所有返回值
我正在尝试使用affiliatewp创建联盟排行榜。我能够得到我想要的每一个值,然而,在我使用的函数中,我使用了一个foreach循环,它调用每个附属公司并显示当月的“销售额”Php Foreach循环只执行第一个返回值,但返回所有返回值,php,wordpress,foreach,affiliate,Php,Wordpress,Foreach,Affiliate,我正在尝试使用affiliatewp创建联盟排行榜。我能够得到我想要的每一个值,然而,在我使用的函数中,我使用了一个foreach循环,它调用每个附属公司并显示当月的“销售额” function affiliate_leaderboard_function() { global $wpdb; $getallreferrals = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_referrals`"); $getallaffilia
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');
功能分支机构\排行榜\功能(){
全球$wpdb;
$getallreferrals=$wpdb->get_results(“从'wp_附属机构\U wp_referrals`中选择*”;
$getallaffiliates=$wpdb->get_results(“从'wp_affiliate_wp_affiliates`中选择*”;
$current_month=获取日期(“m”);
$current_year=获取日期(“Y”);
$current_date=获取_日期(“Y-m-d”);
$lastday=日期('t',标准时间('current_date));
foreach($getallaffiliates作为$theaffiliate){
$user\u id=get\u userdata($theafliate->user\u id);
$userfirstname=$user\u id->first\u name;
$userlastname=$user\u id->last\u name;
$totalreferrals=$theaffiliate->referrals;
$affiliate\u id=$theaffiliate->affiliate\u id;
$getaffreferrals=$wpdb->get_结果(“从`wp_附属机构`wp_附属机构`wp_附属机构`id`=$affiliate_id`和`date`>='$current_year-$current_month-01:00:00:00'和`date`<'\$current年-$current_月-$lastday:23:59'按`wp_附属机构`wp_附属机构`wp_附属机构`wp_附属机构`id`附属机构`ASC`订购。//按附属机构id获取所有转介
$closerstring=$userfirstname。”|本月销售额:“.count($getaffreferrals)。”
“;
如果(!empty($getaffreferrals)){
echo$closerstring;
}
}
}
添加快捷代码(“附属公司排行榜”、“附属公司排行榜功能”);
因此,当我将短代码放在适当的位置,并使用“echo$closerstring”时,它会吐出所有正确的数据,用户的名字后跟他们在本月的销售额。但是短代码输出在内容的顶部
当我将其切换为“return$closerstring”时,它只返回一个分支机构,而不是foreach循环中的所有分支机构。我不知道如何让它像echo函数那样显示所有值,但我需要它显示在正确的位置…Return中断函数的执行 您需要将输出保存为字符串,并在末尾返回,如下所示:
[...]
$output= '';
foreach ($getallaffiliates as $theaffiliate) {
[...]
if(!empty($getaffreferrals)){
$output .= $closerstring;
}
}
return $output;
}
在每个循环后构建字符串,而不是
echo
,然后在末尾返回它:
$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
$result=”“;//创建一个空字符串
foreach($getallaffiliates作为$theaffiliate){
$user\u id=get\u userdata($theafliate->user\u id);
$userfirstname=$user\u id->first\u name;
$userlastname=$user\u id->last\u name;
$totalreferrals=$theaffiliate->referrals;
$affiliate\u id=$theaffiliate->affiliate\u id;
$getaffreferrals=$wpdb->get_结果(“从`wp_附属机构`wp_附属机构`wp_附属机构`id`=$affiliate_id`和`date`>='$current_year-$current_month-01:00:00:00'和`date`<'\$current年-$current_月-$lastday:23:59'按`wp_附属机构`wp_附属机构`wp_附属机构`wp_附属机构`id`附属机构`ASC`订购。//按附属机构id获取所有转介
$closerstring=$userfirstname。”|本月销售额:“.count($getaffreferrals)。”
“;
如果(!empty($getaffreferrals)){
$result.=$closerstring;//添加数据
}
}
返回$result;//返回完整字符串
在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;
$return='';
foreach($getallaffiliates作为$theaffiliate){
$user\u id=get\u userdata($theafliate->user\u id);
$userfirstname=$user\u id->first\u name;
$userlastname=$user\u id->last\u name;
$totalreferrals=$theaffiliate->referrals;
$affiliate\u id=$theaffiliate->affiliate\u id;
$getaffreferrals=$wpdb->get_结果(“从`wp_附属机构`wp_附属机构`wp_附属机构`id`=$affiliate_id`和`date`>='$current_year-$current_month-01:00:00:00'和`date`<'\$current年-$current_月-$lastday:23:59'按`wp_附属机构`wp_附属机构`wp_附属机构`wp_附属机构`id`附属机构`ASC`订购。//按附属机构id获取所有转介
$closerstring=$userfirstname。”|本月销售额:“.count($getaffreferrals)。”
“;
如果(!empty($getaffreferrals)){
$return_array.=$closerstring;
}
}
return$return\u数组;
追加字符串并返回
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');
功能分支机构\排行榜\功能(){
全球$wpdb;
$getallreferrals=$wpdb->get_results(“从'wp_附属机构\U wp_referrals`中选择*”;
$getallaffiliates=$wpdb->get_results(“从'wp_affiliate_wp_affiliates`中选择*”;
$current_month=获取日期(“m”);
$current_year=获取日期(“Y”);
$current_date=获取_日期(“Y-m-d”);
$lastday=日期('t',标准时间('current_date));
$returnstring='';
外汇($GE1talliates作为$theafliate){
$user\u id=get\u userdata($theafliate->user\u id);
$userfirstname=$user\u id->first\u name;
$userlastname=$user\u id->last\u name;
$totalreferrals=$theaffiliate->referrals;
$affiliate\u id=$theaffiliate->affiliate\u id;
$getaffreferrals=$wpdb->get_结果(“从`wp_附属机构`wp_附属机构`wp_附属机构`id`=$affiliate_id`和`date`>='$current_year-$current_month-01:00:00:00'和`date`<'\$current年-$current_月-$lastday:23:59'按`wp_附属机构`wp_附属机构`wp_附属机构`wp_附属机构`id`附属机构`ASC`订购。//按附属机构id获取所有转介
$closerstring=$userfirstname。”|本月销售额:“.count($getaffreferrals)。”
“;
如果(!empty($getaffreferrals)){
$returnstring.=$closerstring;
}
}
return$returnstring;
}
添加快捷代码(“附属公司排行榜”、“附属公司排行榜功能”);