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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行