我是一个前端开发人员,玩后端所以请耐心等待。我只是创建一个简单的应用程序,显示足球比赛的团队阵容。我已经在数据库中创建了表格,并且可以遍历所有22个玩家进行比赛。但是,我只想在一个div中显示前11个而在另一个div中显示第二个11。
到目前为止,这是我的代码:
<?php
try {
$handler = new PDO('mysql:host=localhost;dbname=euro_missing_men','root','');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
die('Sorry, database issue');
}
$query = $handler->query('SELECT * FROM `game` g
JOIN team t on (g.id = t.game_id)
JOIN team_name tn on (t.team_id = tn.id)
JOIN position p on (t.position_id = p.id)
');
$results = $query->fetch(PDO::FETCH_OBJ);
while($row = $query->fetch(PDO::FETCH_OBJ)) {
echo $row->player, "<br/>";
}
?>
这会产生以下结果:
McKimmie
Calderwood
Hendry
Boyd
McKinlay
McCall
McAllister
Collins
Spencer
Durie
Seaman
Neville
Adams
Southgate
Pearce
Anderton
Ince
Gascoigne
McManaman
Sheringham
Shearer
但我想将它们分开。我知道可能有更好的方法来做这个,但为了练习的目的,我想在一个div中回应前11个名字,在另一个div中回应第二个11个名字。
感谢。
答案 0 :(得分:3)
// create a counter
$i = 0;
// echo the opening div
echo "<div>";
while($row = $query->fetch(PDO::FETCH_OBJ)) {
echo $row->player, "<br/>";
if ( $i++ == 11 ) echo "</div><div>"; // increment the counter and at the 11th iteration close the div and start a new one
}
echo "</div>";
答案 1 :(得分:2)
这假设你可能有22个以上。如果你只有22个,那么@pamblam有更简单的答案。
如果您想将它们分成11组,您可以使用modulus运算符:
var $count = 1;
while($row = $query->fetch(PDO::FETCH_OBJ)) {
// After every 11th row, echo a closing div / open a new div
if ( ! ($count++ % 11) ) {
echo '</div><div>';
}
// Don't use break tags, you can't style them well.
echo '<p>' . $row->player . "</p>";
}
或者,另一种方法是将结果分成数组:
$results = $query->fetchAll();
$divs = array_chunk($results, 11);
foreach( $divs AS $div ) {
echo '<div>';
foreach ( $div AS $row ) {
echo '<p>' . $row->player . '</p>';
}
echo '<div>';
}
答案 2 :(得分:1)
简单分割结果?然后循环每一个?
$results = $query->fetchAll();
$team1 = array_slice($results, 0, 11);
$team2 = array_slice($results, 11);
如建议的array_chunck
$results = $query->fetchAll();
$teams = array_chunk($results, 11);
并输出此
$results = $query->fetchAll(PDO::FETCH_OBJ);
$teams = array_chunk($results, 11);
foreach($teams as $team) {
echo '<div>';
foreach($team as $player) {
echo $player->name . '<br />';
}
echo '</div>';
}
答案 3 :(得分:0)
你总是可以做一些基本的事情:
<?php
try {
$handler = new PDO('mysql:host=localhost;dbname=euro_missing_men','root','');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
die('Sorry, database issue');
}
$query = $handler->query('SELECT * FROM `game` g
JOIN team t on (g.id = t.game_id)
JOIN team_name tn on (t.team_id = tn.id)
JOIN position p on (t.position_id = p.id)
');
$results = $query->fetch(PDO::FETCH_OBJ);
$upto = 0;
echo "<div>"; //Start the divs
while($row = $query->fetch(PDO::FETCH_OBJ)) {
$upto ++; //add 1 to upto
if($upto % 11 == 0) //if 11 results
echo "</div><div>"; //create new div
echo $row->player, "<br/>";
}
echo "</div>"; //close div
?>
答案 4 :(得分:0)
您可以添加增量。如果$ i&lt; = 11将玩家放入第一个div。换句话说,把它们放到第二个div。
$results = $query->fetch(PDO::FETCH_OBJ);
$i = 0;
while($row = $query->fetch(PDO::FETCH_OBJ)) {
$i++;
if($i <= 11){
echo "{$i} of 22".$row->player."<br/>";
}
else{
echo "{$i} of 22".$row->player."<br/>";
}
}