如何在不进行表循环的情况下循环获取结果

时间:2015-09-06 02:40:13

标签: php html while-loop html-table

我正在设计一张桌子,我遇到了<th>的困难。 <th>是我数据库中的用户名,因此我需要获取它们,以便它们甚至可以显示在页面上。我目前正在通过fetch while循环从数据库表中获取所有内容。每当我把<th>放在我的循环中时,它就会破坏我桌子的结构。

我正在尝试创建一个看起来像这样的表......

Rnd username1 username2 username3

1名玩家1名玩家1名玩家1

2 player2 player2 player2

3 player3 player3 player3

但是,表格看起来像这样......

enter image description here

我的代码看起来像这样

<table class="draft_border_table">
        <tr>
            <th>Rnd</th>

        </tr>
<?php
$count = 1;
while($draft_order_row = mysqli_fetch_array($draft_order_stmt)) {
    $count + 1;
    $username = $draft_order_row['username'];
    $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'];
?>
        <th><?php echo "<div>" . $username . "</div>"; ?></th>      
        <tr>
            <td><?php echo $count; ?></td>
        </tr>
        <tr>
            <td><?php echo "<div class='draftBorder'>" . $player1 . "</div>"; ?></td>
        </tr>
        <tr>
            <td><?php echo "<div class='draftBorder'>" . $player2 . "</div>"; ?></td>
        </tr>
        <tr>
            <td><?php echo "<div class='draftBorder'>" . $player3 . "</div>"; ?></td>
        </tr>
        <tr>
            <td><?php echo "<div class='draftBorder'>" . $player4 . "</div>"; ?></td>
        </tr>
<?php
//etc table rows for players
}
?>
    </table>

我如何仍然将我的用户名作为循环获取结果的循环并且不在表的循环中?

更新

基于答案的新照片。

enter image description here

user_players
CREATE TABLE `user_players` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `user_id` int(11) NOT NULL,
 `firstname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `lastname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player1` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player2` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player3` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player4` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player5` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player6` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player7` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player8` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player9` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player10` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player11` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player12` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player13` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 `player14` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=284 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

1 个答案:

答案 0 :(得分:0)

您需要将<th>包裹在<tr>中。 <th>表示标题中的单元格。不是整个标题。

您应该生成如下所示的HTML:

<tr>
  <th>username1</th>
  <th>username2</th>
  <th>username3</th>
</tr>