我有下表从我的数据库中查询用户,然后在每个用户下添加14个玩家。我在同一个数据库表中添加了几个列,我试图让列回显,并且它抛出一个未定义的索引错误,但我不完全确定在哪里可以定义它。
这是我试图让新列回应的部分。我有14个玩家,所以这个阵列为每个用户运行。我正在添加相同数量的'职位'所以它将是player1 position1,player2 position2等等。
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
echo '<td><div class="draftBorder">' . $userPlayer['player' . $playerNum] . '</div></td>';
我试图这样做,这就是我收到错误的原因......
foreach ($userPlayerStore as $userPlayer) {
echo '<td><div class="draftBorder">' . $userPlayer['player' . ' - ' . 'position' . $playerNum] . '</div></td>';
我还能如何格式化这个,以便我可以让位置显示在播放器旁边。像这样:
Player1 - Position1
完整代码:
<?php $userPlayerStore = array(); ?>
<table class="draft_border_table">
<tr>
<th>Rnd</th>
<?php
// Output usernames as column headings
$userResults = mysqli_query($con, 'SELECT * FROM user_players ORDER BY `id`');
while($userPlayer = mysqli_fetch_array($userResults)) {
$userPlayerStore[] = $userPlayer;
echo '<th><div>' . $userPlayer['username'] . '</div></th>';
}
?>
</tr>
<?php
// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
if (empty($userPlayer['player'.$playerNum])){
$extra_class = 'emptycell';
}else{
$extra_class = 'filledcell';
}
echo '<td class="draft_table_td">' . $userPlayer['player' . $playerNum] . '</td>';
}
echo '</tr>';
}
?>
</table>
答案 0 :(得分:0)
$ userPlayer var是数据库表中每条记录的反映,此数组中的每个索引都是列名。以下代码可以帮助您:
// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
// Start the player row
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
// Retrieve extra class name? Did you mean to use this?
$extraClass = empty($userPlayer['player' . $playerNum]) ? 'emptycell' : 'filledcell';
// Output player name and position
$playerName = $userPlayer['player' . $playerNum];
$playerPosition = $userPlayer['position' . $playerNum];
echo '<td class="draft_table_td">' . $playerName . ' - ' . $playerPosition . '</td>';
}
// End the player row
echo '</tr>';
}