我想要达到的目标:
在(Position Here)
消息中显示$currentSchoolPosition[0];
值,例如3rd
。
当在foreach
上面使用$currentSchoolPosition[0];
字符串时,如果我将字符串移动到foreach
下面,则值显示array();
,例如3rd Position
显示。
如何获得在foreach之前显示的位置值?
提前感谢!
PHP<?php
/* Current User ID */
$currentUserID = get_current_user_id();
/* Current User SchoolName */
$currentSchoolName = get_the_author_meta( 'School-Name', $user_ID);
/* Current User SchoolRegion */
$currentSchoolRegion = get_the_author_meta( 'School-Region', $user_ID);
/* Current User SchoolPosition Array - Empty */
$currentSchoolPosition = array();
/* Message */
echo '<p>'.$currentSchoolRegion.' Leaderboard</p>';
echo '<p>Your school <strong>'.$currentSchoolName.'</strong> is currently in (Position Here) position</p>';/* POSITION HERE */
/* Get 'Subscriber' Users */
$subscribers = get_users('role=subscriber');
/* Subscriber School Array - Empty */
$schoolArray = array();
/* Counter - Start at '1' */
$i = 1;
/* Table */
echo '<table class="table table-striped">';
foreach ($subscribers as $subscriber) {
$schoolName = $subscriber->get( 'School-Name' );
$schoolRegion = $subscriber->get( 'School-Region' );
/* If 'subscriber' region = current user region push school name(s) to 'schoolArray' */
if($schoolRegion === $currentSchoolRegion){
array_push($schoolArray, $schoolName);
}
}
/* Count matching 'schoolArray' values */
$subscriberValues = array_count_values($schoolArray);
/* Reverse 'schoolArray' order - High to Low */
arsort($subscriberValues);
/* Reset Counter value - Start at '1' */
$i = 1;
foreach ($subscriberValues as $key => $value) {
$counter = $i++;
echo '<tr>';
/* If 'counter' = '1' display 'Champion School' else display 'counter' value */
if ($counter === 1) {
echo '<th scope="row">Champion School</th>';
echo '<td>'.$key.'</td>';
echo '<td>'.$value.' Sign Ups</td>';
} else {
echo '<th scope="row">'.$counter.'</th>';
echo '<td>'.$key.'</td>';
echo '<td>'.$value.' Sign Ups</td>';
}
echo '</tr>';
/* Get current school position */
if($key === $currentSchoolName){
array_push($currentSchoolPosition, $counter);
}
}
echo '</table>';
echo $currentSchoolPosition[0].' Position'; /* POSITION VALUE HERE */
?>
答案: -来自Alan Machado的建议!
<?php
/* Current User ID */
$currentUserID = get_current_user_id();
/* Current User School Name */
$currentSchoolName = get_the_author_meta('School-Name', $user_ID);
/* Current User School Region */
$currentSchoolRegion = get_the_author_meta('School-Region', $user_ID);
/* Current School Position */
$currentSchoolPosition = array();
/* Get Subscribers */
$subscribers = get_users('role=subscriber');
/* Subscriber School Array */
$schoolArray = array();
/* Foreach Subscribers as Subscriber */
foreach ($subscribers as $subscriber) {
/* Subscriber School Name */
$schoolName = $subscriber->get('School-Name');
/* Subscriber School Region */
$schoolRegion = $subscriber->get('School-Region');
/* If Subscriber School Region matches Current User School Region PUSH School Name to Array */
if ($schoolRegion === $currentSchoolRegion) {
array_push($schoolArray, $schoolName);
}
}
/* Reverse School Array Order - High to Low */
arsort($subscriberValues);
/* Count Matching Schoole Array Values */
$subscriberValues = array_count_values($schoolArray);
/* Foreach Get Current School Position */
$i = 1;
foreach ($subscriberValues as $key => $value) {
$counter = $i++;
if ($key === $currentSchoolName) {
array_push($currentSchoolPosition, $counter);
}
}
/* Header Message */
echo '<p>'.$currentSchoolRegion.' Leaderboard</p><p>Your school <strong>'.$currentSchoolName.'</strong> is currently in '.ordinal($currentSchoolPosition[0]).' position</p>';
/* School Leaderboard Table */
echo '<table class="table table-striped">';
$i = 1;
foreach ($subscriberValues as $key => $value) {
$counter = $i++;
echo '<tr>';
/* If 'counter' = '1' display 'Champion School' else display 'counter' value */
if ($counter === 1) {
echo '<th scope="row">Champion School</th><td>'.$key.'</td><td>'.$value.' Sign Ups</td>';
} else {
echo '<th scope="row">'.$counter.'</th><td>'.$key.'</td><td>'.$value.' Sign Ups</td>';
}
echo '</tr>';
}
echo '</table>';
/* Current School Position - Format */
function ordinal($number){
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number % 100) <= 13)) {
return $number.'th';
} else {
return $number.$ends[$number % 10];
}
}
?>
您可以先构建表(通过foreach),然后在使用了从循环创建的数据之后,在最后回显它—像这样:
<?php
/* Current User ID */
$currentUserID = get_current_user_id();
/* Current User SchoolName */
$currentSchoolName = get_the_author_meta( 'School-Name', $user_ID );
/* Current User SchoolRegion */
$currentSchoolRegion = get_the_author_meta( 'School-Region', $user_ID );
/* Current User SchoolPosition Array - Empty */
$currentSchoolPosition = array();
/* Get 'Subscriber' Users */
$subscribers = get_users( 'role=subscriber' );
/* Subscriber School Array - Empty */
$schoolArray = array();
/* Counter - Start at '1' */
$i = 1;
/* Table */
$table = '<table class="table table-striped">';
foreach ( $subscribers as $subscriber ) {
$schoolName = $subscriber->get( 'School-Name' );
$schoolRegion = $subscriber->get( 'School-Region' );
/* If 'subscriber' region = current user region push school name(s) to 'schoolArray' */
if ( $schoolRegion === $currentSchoolRegion ) {
array_push( $schoolArray, $schoolName );
}
}
/* Count matching 'schoolArray' values */
$subscriberValues = array_count_values( $schoolArray );
/* Reverse 'schoolArray' order - High to Low */
arsort( $subscriberValues );
/* Reset Counter value - Start at '1' */
$i = 1;
foreach ( $subscriberValues as $key => $value ) {
$counter = $i++;
$table .= '<tr>';
/* If 'counter' = '1' display 'Champion School' else display 'counter' value */
if ( $counter === 1 ) {
$table .= '<th scope="row">Champion School</th>';
$table .= '<td>' . $key . '</td>';
$table .= '<td>' . $value . ' Sign Ups</td>';
}
else {
$table .= '<th scope="row">' . $counter . '</th>';
$table .= '<td>' . $key . '</td>';
$table .= '<td>' . $value . ' Sign Ups</td>';
}
$table .= '</tr>';
/* Get current school position */
if ( $key === $currentSchoolName ) {
array_push( $currentSchoolPosition, $counter );
}
}
$table .= '</table>';
/* Message */
echo '<p>' . $currentSchoolRegion . ' Leaderboard</p>';
echo '<p>Your school <strong>' . $currentSchoolName . '</strong> is currently in (Position Here) position</p>';
/* POSITION HERE */
echo $currentSchoolPosition[0] . ' Position';
/* POSITION VALUE HERE */
// print out the table
echo $table;
?>
<?php
/* Current User ID */
$currentUserID = get_current_user_id();
/* Current User School Name */
$currentSchoolName = get_the_author_meta('School-Name', $user_ID);
/* Current User School Region */
$currentSchoolRegion = get_the_author_meta('School-Region', $user_ID);
/* Current School Position */
$currentSchoolPosition = array();
/* Get Subscribers */
$subscribers = get_users('role=subscriber');
/* Subscriber School Array */
$schoolArray = array();
/* Foreach Subscribers as Subscriber */
foreach ($subscribers as $subscriber) {
/* Subscriber School Name */
$schoolName = $subscriber->get('School-Name');
/* Subscriber School Region */
$schoolRegion = $subscriber->get('School-Region');
/* If Subscriber School Region matches Current User School Region PUSH School Name to Array */
if ($schoolRegion === $currentSchoolRegion) {
array_push($schoolArray, $schoolName);
}
}
/* Reverse School Array Order - High to Low */
arsort($subscriberValues);
/* Count Matching Schoole Array Values */
$subscriberValues = array_count_values($schoolArray);
/* Foreach Get Current School Position */
$i = 1;
foreach ($subscriberValues as $key => $value) {
$counter = $i++;
if ($key === $currentSchoolName) {
array_push($currentSchoolPosition, $counter);
}
}
/* Header Message */
echo '<p>'.$currentSchoolRegion.' Leaderboard</p><p>Your school <strong>'.$currentSchoolName.'</strong> is currently in '.ordinal($currentSchoolPosition[0]).' position</p>';
/* School Leaderboard Table */
echo '<table class="table table-striped">';
$i = 1;
foreach ($subscriberValues as $key => $value) {
$counter = $i++;
echo '<tr>';
/* If 'counter' = '1' display 'Champion School' else display 'counter' value */
if ($counter === 1) {
echo '<th scope="row">Champion School</th><td>'.$key.'</td><td>'.$value.' Sign Ups</td>';
} else {
echo '<th scope="row">'.$counter.'</th><td>'.$key.'</td><td>'.$value.' Sign Ups</td>';
}
echo '</tr>';
}
echo '</table>';
/* Current School Position - Format */
function ordinal($number){
$ends = array('th','st','nd','rd','th','th','th','th','th','th');
if ((($number % 100) >= 11) && (($number % 100) <= 13)) {
return $number.'th';
} else {
return $number.$ends[$number % 10];
}
}
?>