表不输出所有列

时间:2015-09-06 07:07:20

标签: php mysql for-loop while-loop html-table

我正在用这张非常简单的桌子做噩梦。我正在尝试创建一个草稿类型板。我有用户和玩家。我希望用户在每个玩家下显示为<th>,然后显示14个玩家<td>。有点像这样..

http://lockerroomfantasysports.com/wp-content/uploads/2013/08/FantasySports-Fantasy-Football-Draft-Board.jpg

然而,我的页面显示如下...... enter image description here

它只为用户创建两列播放器。而不是每个人一个。它也没有显示db内容应该在哪里。显示的播放器应该只位于第一个位置(左侧)。

这是它的代码。

$draft_order_stmt = mysqli_query($con,"SELECT * FROM user_players ORDER BY `id`");
$draft_order_stmt2 = mysqli_query($con,"SELECT username FROM user_players ORDER BY `id`");

?>
<table class="draft_border_table">
    <tr>
        <th class="draft_table_number_th">RND</th>
<?php

while ($draft_user_row = mysqli_fetch_array($draft_order_stmt2)) {

    $username = $draft_user_row['username'];

    echo "<th class='draft_table_th'><div>" . $username . "</div></th>";

}
?>

    </tr>

<?php
for ($count = 1; $count < 15; $count++) { 

$col = "player" . $count; 
$query = "SELECT $col FROM user_players ORDER BY id"; 
$draft_order_stmt2 = mysqli_query($con, $query); 

$draft_order_row = mysqli_fetch_array($draft_order_stmt2); 

echo "<tr><td>" . $count . "</td>"; 

foreach ($draft_order_row as $player) { 

echo "<td><div class=\"draftBorder\">"; 

if (is_null($player)) {

$player = "&nbsp;";

}

echo $player . "</div></td>"; 

} 

echo "</tr>"; 
}
?>
</table>

我最初也尝试了这个,它显示了所有玩家输入,但玩家输入都在一个区块中。像这样......

user1 user2 user3

所有14个玩家输入

再次全部14个玩家输入

再次全部14个玩家输入

等。

这是我的代码...

$draft_order_stmt = mysqli_query($con,"SELECT * FROM user_players ORDER BY `id`");
$draft_order_stmt2 = mysqli_query($con,"SELECT username FROM user_players ORDER BY `id`");
?>
<table class="draft_border_table">
            <tr>
                <th>Rnd</th>

<?php 
while($draft_username_row = mysqli_fetch_array($draft_order_stmt2)) {
    $username = $draft_username_row['username'];
?>

                <th><?php echo "<div>" . $username . "</div>"; ?></th>
<?php
}
?>
            </tr>
<?php
$count = 1;
while($draft_order_row = mysqli_fetch_array($draft_order_stmt)) {
    $count + 1;
    $player1 = $draft_order_row['player1'];
    $player2 = $draft_order_row['player2'];
    $player3 = $draft_order_row['player3'];
    $player4 = $draft_order_row['player4'];
    $player5 = $draft_order_row['player5'];
    $player6 = $draft_order_row['player6'];
    $player7 = $draft_order_row['player7'];
    $player8 = $draft_order_row['player8'];
    $player9 = $draft_order_row['player9'];
    $player10 = $draft_order_row['player10'];
    $player11 = $draft_order_row['player11'];
    $player12 = $draft_order_row['player12'];
    $player13 = $draft_order_row['player13'];
    $player14 = $draft_order_row['player14'];
?>
            <tr>
                <td><?php echo $count; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player1 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player2 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player3 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player4 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player5 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player6 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player7 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player8 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player9 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player10 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player11 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player12 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player13 . "</div>"; ?></td>

                <td><?php echo "<div class='draftBorder'>" . $player14 . "</div>"; ?></td>
            </tr>

<?php
}
?>
        </table>

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

<?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) {
        echo '<td><div class="draftBorder">' . $userPlayer['player' . $playerNum] . '</div></td>';
    }
    echo '</tr>';
}

?>

</table>