[英]PHP add html break after every 10th mysql result
我的代碼有問題。 我想要達到的目標:
我有一個名為“比賽”的數據庫表,其中包含有關足球比賽器材的信息。 現在我在這張表中大約有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++;
}
我建議使用mod或%符號,這將返回除法的余數(因此,每10行n / 10的余數將為0)。
所以你想要一個櫃台
$count = 1;
while( $row = ..... ){
if( $count % 10 == 0 ){
//do line break;
}
++$count;
}
php關於mod的文檔http://www.php.net/manual/zh/internals2.opcodes.mod.php
您可能需要對此稍作練習,但這是基礎。
我建議我能想到的最簡單的事情。
$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。 如果對$ counter進行10位檢查后,if語句將檢查是否還有余數。如果有余數,它將繼續運行而不會出現換行符。 如果沒有余數,即為10的倍數,則會附加一個
到最后。
希望這就是你想要的!
您將需要計算每一行。 使用從0
開始的新變量$i
,該變量將在每個循環中增加一。 然后檢查該變量是否大於10。如果是,請在$fixtures
字符串中添加更多html,以創建“空白”行。
如果發現空白行,請將$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
}
}
我將查找將OFFSET
與LIMIT
子句一起使用的分頁解決方案。 除此之外,您實際上可以將結果存儲在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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.