PHP每10次mysql结果后添加html break

时间:2014-07-09 21:14:03

标签: php html mysql

我的代码遇到了问题。我想要实现的目标:

我有一个名为"匹配"的数据库表,其中包含有关足球赛程的信息。现在我在这张桌子上有大约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的新手,我要求具体的解决方案。

谢谢。

5 个答案:

答案 0 :(得分:4)

试试这个;)

<?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++;
}

答案 1 :(得分:4)

我建议使用mod或%符号,这将返回除法的余数(因此n / 10的余数将为每10行0)。

所以你想要一个计数器

$count = 1;
while( $row = ..... ){
    if( $count % 10 == 0 ){
     //do line break;
   }


   ++$count;
}

关于mod的php文档 http://www.php.net/manual/en/internals2.opcodes.mod.php

你可能不得不玩这个,但这是基础。

答案 2 :(得分:2)

我建议我能想到最简单的事情。

$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) or die (mysqli_error($sql));
$fixtures='';
$counter = 0;
while($row = mysqli_fetch_array( $query )) { 
$counter++;
$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 (($counter % 10) == 0){ $fixtures .= '<br>'; }
}

每次while迭代时,这将向计数器添加1。 if语句将在检查$ counter 10之后检查是否有余数。如果有一个,它将继续运行而没有换行符。如果没有余数,意味着它是10的倍数,它会在结尾附加一个。

希望这就是你想要的!

答案 3 :(得分:0)

您需要计算每一行。使用从$i开始的新变量0,此变量将在每个循环中增加1。然后检查该变量是否高于10.如果是在$fixtures字符串中添加更多html,则会创建一个&#34;空白&#34;行。

如果找到空白行,还会将$i变量重置为0

$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) or die (mysqli_error($sql));
$fixtures='';
$i = 0; // <-- New variable

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

    $i ++; // <-- add one

    if ($i > 10) { // <-- If larger than 10 then do this statement

        $fixtures.='<tr><td colspan=4>BREAK</td></tr>';
        $i = 0; // <-- Oh and reset $i
    }

}

答案 4 :(得分:0)

我会查找使用OFFSETLIMIT子句的分页解决方案。除此之外,您实际上可以将结果存储在10个组中,然后在循环组时应用输出逻辑:

$sql = "SELECT * FROM matches ORDER BY matchid ASC";
$query = mysqli_query($Connection, $sql) or die (mysqli_error($sql));
$fixtures = array(); // Each element will be an array of fixtures
$fixturesPerGroup = 10; // How many fixtures per group
$fixtureCount = 0; // Counter during the while loop iterating
$fixtureGroup = 0; // Which group to put the fixtures into
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[$fixtureGroup][] ='<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>';

    $fixtureCount++;
    // Once we reach the fixturesPerGroup count, go to the next group
    if (($fixtureCount % $fixturesPerGroup) == 0) {
        $fixtureGroup++;
    }
}

然后你可以遍历群组:

<table>
<?php 
foreach ($fixtures as $fixturesInGroup) {
    // Do logic here for displaying each group
    echo implode('<br />', $fixturesInGroup);
}
?>
</table>