在表格中仅显示一次日期

问题描述 投票:0回答:1

我有一张比赛日桌子。周五、周六、周日都有比赛。 但我不想在周六的每场比赛中显示周六的日期 4x。 应该是这样的

1.10. 
game
2.10.
game
game
game
3.10.
game
game

<?php
        foreach ($matches as $match) {
            ?> 
            <tr> 
<td colspan="5"><?php 

$datetime = new DateTime( "now", new DateTimeZone( "Europe/Bucharest" ) );
setlocale(LC_TIME, 'de_DE', 'deu_deu');
$datetime = new DateTime($match["matchDateTime"]);

echo $datetime->format('j.m. | H:i');  
  
 ?> Uhr</td>
</tr>
<tr>
                <td class="icon"><img src="<?php echo $match["team1"]["teamIconUrl"]; ?>" height="30"></td> 
                <td class="result">
                <div class="result2"><?php echo $match["matchResults"][1]["pointsTeam1"]; ?> : <?php echo $match["matchResults"][1]["pointsTeam2"]; ?></div>  
                </td> 
                <td class="icon"><img src="<?php echo $match["team2"]["teamIconUrl"]; ?>" height="30"></td> 
        
                
            </tr>
            <?php
        }
        ?> 
php
1个回答
0
投票

尝试这样的代码(未经测试)或类似的逻辑。将最后一个日期时间存储在变量中并检查它是否已更改:

<?php

function getDatetime($match)
{
    $datetime = new DateTime("now", new DateTimeZone("Europe/Bucharest"));
    setlocale(LC_TIME, 'de_DE', 'deu_deu');
    $datetime = new DateTime($match["matchDateTime"]);
    return $datetime->format('j.m. | H:i');
}


$lastDatetime = null;
foreach ($matches as $match) {
    $datetime = getDatetime($match);
    if($lastDatetime != $datetime) {
        $lastDatetime = $datetime;
        ?>
            <tr><td colspan='5'><?=$datetime;?> Uhr</td></tr>
        <?php
    }
    ?>
    <tr>
        <td class="icon"><img src="<?=$match["team1"]["teamIconUrl"]; ?>" height="30"></td>
        <td class="result">
            <div class="result2"><?=$match["matchResults"][1]["pointsTeam1"]; ?> :
                <?=$match["matchResults"][1]["pointsTeam2"]; ?>
            </div>
        </td>
        <td class="icon"><img src="<?=$match["team2"]["teamIconUrl"]; ?>" height="30"></td>


    </tr>
    <?php
}
?>

对未来的一些提示:

  • 尽量不要将 PHP 逻辑与 HTML 混合在一起(或者至少将其最小化到 开始)。
  • 当你想显示 PHP 时,不需要在 HTML 中使用 echo 多变的。您可以使用
    <?=$var; ?>
    来代替。
  • 对于
    <td colspan='5'>
    ,如果您的中没有更多元素 表比你的问题中的,它不应该是 5,而是 您希望单个单元格跨越的列数。
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.