How to get <th> to loop through fetch results without being in a table loop [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-13 11:04:02

问题


I am designing a table and I am having difficulties with a <th>. The <th> is usernames from my database, so I need to fetch them, so that they can even display on the page. I am currently fetching everything from my database table through a fetch while loop. Whenever, I put the <th> in my loop it ruins the structure of my table.

I am trying to create a table that looks like this...

Rnd username1 username2 username3

1 player 1 player1 player1

2 player2 player2 player2

3 player3 player3 player3

However, the table looks like this...

My code looks like this

<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>

How can I still have my usernames as my loop through the fetched results and be out of the loop for the table?

UPDATE

New pic based on an answer.

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:


You need to wrap your <th>s in <tr>s. <th> denotes a cell in the header. Not the whole header.

You should produce HTML that looks like this:

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


来源:https://stackoverflow.com/questions/32419503/how-to-get-th-to-loop-through-fetch-results-without-being-in-a-table-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!