我的代码有问题。我想要达到的目标:

我有一个名为“比赛”的数据库表,其中包含有关足球比赛器材的信息。现在我在这张表中大约有100个匹配项,但很快就会达到500个以上,为了不让观众震惊,我希望我的代码执行换行符(或者可能是一个新的表格行,那现在不重要了,只是每10个结果后显示一些HTML标签)(因为每周有10个匹配项)

这是我的PHP代码:

$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) or die (mysqli_error($sql));
$fixtures='';
while($row = mysqli_fetch_array( $query )) {
    $fixturetime = $row["time"];
    $fixturetime2 = $row["time"];
    $fixtureid = $row["matchid"];
    $fixtureround = $row["round"];
    $hometeamid = $row["hometeamid"];
    $awayteamid = $row["awayteamid"];
    $fixturetime = strtotime( $fixturetime );
    $fixturetime = date( 'd.m.Y', $fixturetime );
    $fixturetime2 = strtotime( $fixturetime2 );
    $fixturetime2 = date( 'H:i', $fixturetime2 );

    $fixtures.='<tr><td><strong>' .$hometeamname. '</strong> v. <strong>' .$awayteamname. '</strong></td><td>' .$fixturetime. '</td><td>' .$fixturetime2. '</td><td style="text-align: right;">' .$fixtureid. '</td><td>' .$i. '</td></tr>';
}


这是HTML输出:

<table>
    <?php echo $fixtures; ?>
</table>


我尝试了一些在stackoverflow上显示的方法,但无法将其正确实现到我的解决方案中。因为我对PHP有点陌生,所以我要求特定的解决方案。

谢谢。

最佳答案

尝试这个 ;)

<?php

$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) or die (mysqli_error($sql));
$fixtures = '';
$i = 1;

while($row = mysqli_fetch_array($query)) {
    $fixturetime = $row["time"];
    $fixturetime2 = $row["time"];
    $fixtureid = $row["matchid"];
    $fixtureround = $row["round"];
    $hometeamid = $row["hometeamid"];
    $awayteamid = $row["awayteamid"];
    $fixturetime = strtotime($fixturetime);
    $fixturetime = date('d.m.Y', $fixturetime);
    $fixturetime2 = strtotime($fixturetime2);
    $fixturetime2 = date('H:i', $fixturetime2);

    $fixtures .= '<tr><td><strong>' . $hometeamname . '</strong> v. <strong>' . $awayteamname . '</strong></td><td>' . $fixturetime . '</td><td>' . $fixturetime2 . '</td><td style="text-align: right;">' . $fixtureid . '</td><td>' . $i . '</td></tr>';

    if(!($i % 10)){
        $fixtures .= '<!-- break -->';
    }

    $i++;
}

关于php - PHP每隔10个mysql结果添加html中断,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24663919/

10-12 21:40