PHP foreach if/else with a table


PHP foreach if/else with a table

我的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"一定有什么东西,但无法找到解决方案。

任何帮助都非常感谢。非常感谢

您必须在循环之前将布尔值设置为"false",这意味着"未找到周年纪念日"。如果找到任何内容,请将其设置为"true":

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

使用计数器很简单:

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

更改 :

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

}

检查 $jahre->y 的最后一个值。它不会等于$jubi.因此,for 循环的最后一次迭代会导致此问题。

更改行,如下所示:

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