我想要实现的目标:
在$currentSchoolPosition[0];
消息中显示3rd
值,例如(Position Here)
。
当前问题:
当使用$currentSchoolPosition[0];
上方的foreach
字符串时,如果我将字符串移到array();
下方,foreach
显示3rd Position
,则会显示<?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 */
?>
。
如何在foreach之前显示位置值?
提前谢谢!
PHP
<?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];
}
}
?>
Anwser: - 来自Alan Machado的建议!
countryCode
答案 0 :(得分:0)
您可以先构建表(通过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;
?>
答案 1 :(得分:0)
<?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];
}
}
?>