我的Wordpress网站上有一个PHP脚本,我希望在其中显示我所有注册俱乐部会员的10周年纪念日。如果今年没有周年纪念日,则会显示一条消息,上面写着“今年没有纪念日”。到目前为止我做了什么:
<?php
$args2 = array(
'role' => '',
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$members = get_users($args2);
echo '<table cellpadding="0" cellspacing="0" style="margin-top:20px;">';
foreach ($members as $user) {
$clubeintritt = new DateTime($user->club); // club entry date
$jahre = $clubeintritt->diff(new DateTime); // no. of years in the club
$jubi=('10'); // 10 years
if($jahre->y == $jubi) {
echo '<tr>';
echo '<td>' . $user->first_name . ' ' .$user->last_name .'</td>';
echo '</tr>';
}
}
if($jahre->y != $jubi) {
echo 'No Aniversary this yeaar';
}
echo '</table>';
?>
现在,我获得了10年俱乐部成员的名单,但也传达了今年没有周年纪念日的信息。
我想必须有“endforeach”的东西,但无法找到解决方案。
非常感谢任何帮助。 非常感谢
答案 0 :(得分:0)
您必须将布尔值设置为&#34; false&#34;在你的循环之前,意思是&#34;没有找到周年纪念日&#34;。如果您找到,请将其设置为&#34; true&#34;:
echo '<table cellpadding="0" cellspacing="0" style="margin-top:20px;">';
$found = false;
foreach ($members as $user) {
$clubeintritt = new DateTime($user->club); // club entry date
$jahre = $clubeintritt->diff(new DateTime); // no. of years in the club
$jubi=('10'); // 10 years
if($jahre->y == $jubi) {
$found = true;
echo '<tr>';
echo '<td>' . $user->first_name . ' ' .$user->last_name .'</td>';
echo '</tr>';
}
}
if(!$found) {
echo 'No Aniversary this yeaar';
}
echo '</table>';
答案 1 :(得分:0)
使用计数器很简单:
<?php
$args2 = array(
'role' => '',
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$members = get_users($args2);
$aCounter=0;
echo '<table cellpadding="0" cellspacing="0" style="margin-top:20px;">';
foreach ($members as $user) {
$clubeintritt = new DateTime($user->club); // club entry date
$jahre = $clubeintritt->diff(new DateTime); // no. of years in the club
$jubi=('10'); // 10 years
if($jahre->y == $jubi) {
$aCounter++;
echo '<tr>';
echo '<td>' . $user->first_name . ' ' .$user->last_name .'</td>';
echo '</tr>';
}
}
if($aCounter === 0) {
echo 'No Aniversary this yeaar';
}
echo '</table>';
?>
答案 2 :(得分:0)
更改:
echo '<table cellpadding="0" cellspacing="0" style="margin-top:20px;">';
foreach ($members as $user) {
$clubeintritt = new DateTime($user->club); // club entry date
$jahre = $clubeintritt->diff(new DateTime); // no. of years in the club
$jubi=('10'); // 10 years
if($jahre->y == $jubi) {
echo '<tr>';
echo '<td>' . $user->first_name . ' ' .$user->last_name .'</td>';
echo '</tr>';
}
}
if($jahre->y != $jubi) {
echo 'No Aniversary this yeaar';
}
echo '</table>';
到
$out = '';
foreach ($members as $user) {
$clubeintritt = new DateTime($user->club); // club entry date
$jahre = $clubeintritt->diff(new DateTime); // no. of years in the club
$jubi=('10'); // 10 years
if($jahre->y == $jubi) {
$out .= '<tr>';
$out .= '<td>' . $user->first_name . ' ' .$user->last_name .'</td>';
$out .= '</tr>';
}
}
if(empty($out)) {
echo 'No Aniversary this yeaar';
}
else
{ echo '<table cellpadding="0" cellspacing="0" style="margin-top:20px;">';
echo $out;
echo '</table>';
}
答案 3 :(得分:0)
检查ws.Range("H3:M7).Copy()
ws.Range("H3").Insert(Shift="xlShiftDown")
的最后一个值。它不等于$jahre->y
。因此,for循环的最后一次迭代会导致问题。
答案 4 :(得分:0)
更改以下行:
$members = get_users($args2);
$aniversary = false;
echo '<table cellpadding="0" cellspacing="0" style="margin-top:20px;">';
foreach ($members as $user) {
$clubeintritt = new DateTime($user->club); // club entry date
$jahre = $clubeintritt->diff(new DateTime); // no. of years in the club
$jubi=('10'); // 10 years
if($jahre->y == $jubi) {
echo '<tr>';
echo '<td>' . $user->first_name . ' ' .$user->last_name .'</td>';
echo '</tr>';
$aniversary = true;
}
}
if(!$aniversary) {
echo 'No Aniversary this yeaar';
}
echo '</table>';
?>