[英]PHP loop through mysql results with two selects and create an aligned html table
我在创建在PHP中像这样对齐的表时遇到了问题:
TH HOME AWAY
TH | num | name | teamname | | num | name | teamname |
TR | hsknum | hskname | hsktname | | asknum | askname | asktname |
我发现无法获得两个结果以foreach或while循环在表中对齐。
编辑2:
HOME或AWAY可能为空,或1-> 14个条目(HOME或AWAY的结果),这就是我的代码技能瓦解的地方。
谢谢。
编辑1:
我的PHP循环是:
<?php while (( $resultshome = $rowh->fetch()) || ($resultsaway = $rowa->fetch())) { ?>
<tr>
<td>
<?php
if ( !empty($resultshome) ) { ?>
<?php echo $resultshome['hsknum']; ?>
</td>
<td>
<a href="/runner/profiles/runnerprofile.php?runnerid=<?php echo $resultshome['hskid']; ?>"><?php echo $resultshome['hskname']; ?></a>
</td>
<td>
<?php echo $resultshome['hsktname']; ?>
</td>
<td>
</td>
<td>
</td>
<?php
}
else {
echo "<td></td><td></td><td></td><td></td>";
} ?>
<td>
<?php
if ( !empty($resultsaway) ) { ?>
<?php echo $resultsaway['asknum']; ?>
</td>
<td>
<a href="/runner/profiles/runnerprofile.php?runnerid=<?php echo $resultsaway['askid']; ?>"><?php echo $resultsaway['askname']; ?></a>
</td>
<td>
<?php echo $resultsaway['asktname']; ?>
</td>
</tr>
<?php
}
else {
echo "<td></td><td></td>";
} ?>
<?php } ?>
</table>
我想你想打开:
while (( $resultshome = $rowh->fetch()) && ($resultsaway = $rowa->fetch()))
PHP会使布尔语句短路,这意味着在(A || B)情况下,仅当“ A”的值为假时,才可以到达“ B”。 只要条件之间具有OR,就可以在开始$ rowa之前遍历所有$ rowh。
我稍微整理了一下代码,然后将任务移出了循环。
<table>
<?php
$keys = [
"home" => [
'hsknum',
'hskid',
'hskname',
'hsktname',
],
"away" => [
'asknum',
'askid',
'askname',
'asktname',
],
];
$myFunc = function($row, $inArray, $team) use ($keys) {
if (!empty($inArray)) {
array_push(
$row,
$inArray[$keys[$team][0]],
sprintf(
'<a href="/runner/profiles/runnerprofile.php?runnerid=%s">%s</a>',
$inArray[$keys[$team][1]],
$inArray[$keys[$team][2]]
),
$inArray[$keys[$team][3]],
" ",
" "
);
} else {
$row = array_merge($row, array_fill(0, 5, " "));
}
return $row;
};
do {
$resultshome = $rowh->fetch();
$resultsaway = $rowa->fetch();
if (!$resultshome && !$resultsaway) {
break;
}
$row = [];
$row = $myFunc($row, $resultshome, "home");
$row = $myFunc($row, $resultsaway, "away");
echo "<tr><td>", implode("</td><td>", $row), "</td></tr>";
} while (true);
?>
</table>
编辑:现在有了home和away阵列键...
我对此游戏一无所知,但数据库应该像这样:
game
id
homeTeamId
awayTeamId
fromDate
toDate
team
id
name
player
id
name
team_player
teamId
playerId
fromDate
toDate
team_game_players
gameId
teamId
playerId
number?
game_score
gameId
teamId
playerId
something?
更好的结构是将获取逻辑与显示逻辑分开。 如果两个查询都可以返回不同的行数(例如10个主场比赛对5个客场比赛),则尤其如此
$games = array();
$row = 0;
# fetch home game data
while($row = $rowh->fetch()) {
$games[$row] = array(); // initialize this array row
$games[$row]['home'] = $row;
$row++;
}
$row = 0;
# fetch away game data
while($row = $rowa->fetch()) {
$games[$row]['away'] = $row;
$row++;
}
foreach ($games as $index => $game) {
if (isset($game['home']) {
... output home stuff ...
} else {
... output empty cells - no home game for this particular row
}
if (isset($game['away']) {
... output away stuff
} else {
... output empty cells - no away game for this particular row
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.