dsbgltg159136540 2016-04-28 15:17
浏览 47
已采纳

PHP foreach if / else with table

I have a PHP script on my Wordpress Site, where I want to show the 10 member anniversary of all my registered club members. If there's no anniversary in the current year, a message shall show up saying something like "No Anniversary this year". What I have done so far:

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

So right now, I get the list with my 10 years club members, but also the message that there's no anniversary this year.

I guess there must be something with "endforeach", but couldn't come to a solution.

Any help is highly appreciated. Many thanks

  • 写回答

5条回答 默认 最新

  • donglan6967 2016-04-28 15:23
    关注

    You have to set a boolean to "false" before your loop, meaning "No anniversary found". If you find any, you set it to "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>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog