我是PHP的新手。 我试图将游戏(名称)放在5行5列表的5x5表中。 但我想通过游戏的数字输出生成表,而不是预设的html表。 我让它显示游戏,但不是在表格中。 能帮助我,给我一个很好的解释,所以我也理解。 谢谢你已经!
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Merijn</title>
</head>
<body>
<?php
$games = array(
"battlefield 1",
"battlefield 2",
"Out of the Park Baseball 17",
"Overwatch",
"Stephen's Sausage Roll",
"Dark Souls III",
"Kentucky Route Zero - Act IV NEW",
"Stardew Valley",
"Ori and the Blind Forest",
"XCOM 2",
"World of Warcraft: Legion",
"Steins;Gate ",
"The Witness",
"Inside",
"Hex: Shards of Fate",
"Forza Horizon 3 ",
"Rise of the Tomb Raider",
"Total War: WARHAMMER",
"Chime Sharp",
"Pony Island",
"F1 2016",
"Day of the Tentacle Remastered",
"Tales from the Borderlands",
"DOOM",
"Hearthstone");
for($i =0; $i <=25; $i++){
echo '<table rows=5 cols=5>';
echo "<td>" . $games[$i] . "</td>" . "<br/>";
}
?>
</body>
</html>
答案 0 :(得分:2)
在您的代码中,您必须在循环外使用<table>
,而不是必须定义5列,而不是根据需要使用$games
数组:
示例:强>
<?php
$games = array(
"battlefield 1",
"battlefield 2",
"Out of the Park Baseball 17",
"Overwatch",
"Stephen's Sausage Roll",
"Dark Souls III",
"Kentucky Route Zero - Act IV NEW",
"Stardew Valley",
"Ori and the Blind Forest",
"XCOM 2",
"World of Warcraft: Legion",
"Steins;Gate ",
"The Witness",
"Inside",
"Hex: Shards of Fate",
"Forza Horizon 3 ",
"Rise of the Tomb Raider",
"Total War: WARHAMMER",
"Chime Sharp",
"Pony Island",
"F1 2016",
"Day of the Tentacle Remastered",
"Tales from the Borderlands",
"DOOM",
"Hearthstone");
?>
<table border="1">
<tr>
<?php
// this will print 5 columns
for ($i=1; $i <= 5 ; $i++) {
?>
<td><?php echo "Column". $i ?></td>
<?php
}
?>
</tr>
<?php
// this will print your games value in 5 rows each
$games = array_chunk($games, 5); // break in chunks
foreach ($games as $key => $value) {
echo "<tr>"; // starting tr for values
foreach ($value as $fvalue) { // this will break in 5 rows each.
?>
<td><?=$fvalue?></td>
<?php
}
echo "</tr>"; // closing tr
}
?>
</table>
<强> DEMO 强>
答案 1 :(得分:1)
也许不是最干净但它有效:)
<?php
$games = array(
"battlefield 1",
"battlefield 2",
"Out of the Park Baseball 17",
"Overwatch",
"Stephen's Sausage Roll",
"Dark Souls III",
"Kentucky Route Zero - Act IV NEW",
"Stardew Valley",
"Ori and the Blind Forest",
"XCOM 2",
"World of Warcraft: Legion",
"Steins;Gate ",
"The Witness",
"Inside",
"Hex: Shards of Fate",
"Forza Horizon 3 ",
"Rise of the Tomb Raider",
"Total War: WARHAMMER",
"Chime Sharp",
"Pony Island",
"F1 2016",
"Day of the Tentacle Remastered",
"Tales from the Borderlands",
"DOOM",
"Hearthstone");
$c = 0;
?>
<table>
<tr>
<?php for ($i=0; $i < count( $games ); $i++) {
if( $c == 5 )
{
$c = 0;
?>
</tr>
<tr>
<?php } echo "<td>".$games[ $i ]."</td>"; $c++;
} ?>
</tr>
</table>
答案 2 :(得分:0)
试试这个:
$games = array(
"battlefield 1",
"battlefield 2",
"Out of the Park Baseball 17",
"Overwatch",
"Stephen's Sausage Roll",
"Dark Souls III",
"Kentucky Route Zero - Act IV NEW",
"Stardew Valley",
"Ori and the Blind Forest",
"XCOM 2",
"World of Warcraft: Legion",
"Steins;Gate ",
"The Witness",
"Inside",
"Hex: Shards of Fate",
"Forza Horizon 3 ",
"Rise of the Tomb Raider",
"Total War: WARHAMMER",
"Chime Sharp",
"Pony Island",
"F1 2016",
"Day of the Tentacle Remastered",
"Tales from the Borderlands",
"DOOM",
"Hearthstone");
echo '<table border=1 rows=5 cols=5><tr>';
for($i =0; $i < 25; $i++){
if ($i%5 === 0) {
echo '</tr><tr>';
}
echo "<td>" . $games[$i] . "</td>";
}
echo '</tr></table>';