PHP在每10次mysql结果后添加html break [英] PHP add html break after every 10th mysql result

查看:63
本文介绍了PHP在每10次mysql结果后添加html break的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题。我试图达到的目标:

I am having a trouble with my code. What I am trying to achieve:

我有一个名为matches的数据库表,其中包含有关足球赛的信息。现在我在这张桌子上有大约100场比赛,但很快就要达到500+,所以为了不打击我的观众的头脑,我希望我的代码执行换行符(或者新的表格行,那现在不重要,只是(因为每周都会有10个匹配项)

I have a database table called "matches", containing info about football fixtures. For now i have around 100 matches in this table, but soon to be 500+, so to not to blow my viewers minds, i want my code to perform a line break (or maybe a new table row, thats not important now, just some HTML tag) after every 10th result is shown(because every week there are 10 matches)

这是我的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有点新鲜,所以我正在寻求特定的解决方案。

I have tried some methods shown on stackoverflow, but I was not able to implement them properly to my solution. Because I am kinda new to PHP I am asking for specific solution.

谢谢。

推荐答案

试试这个;)

<?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在每10次mysql结果后添加html break的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆