72379

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

Question:

This question is an exact duplicate of:

<ul><li> <a href="/questions/32419245/issues-getting-table-headers-to-position-correctly" dir="ltr" rel="nofollow">Issues getting table headers to position correctly</a> <span class="question-originals-answer-count"> 1 answer </span> </li> </ul>

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

<a href="https://i.stack.imgur.com/ZTkCc.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/ZTkCc.jpg" data-original="https://i.stack.imgur.com/ZTkCc.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

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.

<a href="https://i.stack.imgur.com/ZTkCc.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/ZTkCc.jpg" data-original="https://i.stack.imgur.com/ZTkCc.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

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

Answer1:

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>

Recommend

  • adding values of dictionaries in python
  • Using base default constructor (with parameters) for child class
  • Does Sqlite database support persian/arabic encoding?
  • How to get to loop through fetch results without being in a table loop [duplicate]
  • Use of BigInteger.isProbablePrime() to generate cryptographically secure primes
  • Issues adding a set of database columns to array
  • Extracting variables from Javascript inside HTML
  • Random element from array in Excel-VBA
  • Android - Generate non-repetitive random numbers [duplicate]
  • Calculated 'Across' Variable in PROC REPORT
  • Emulate Python's random.choice in .NET
  • aws cognito pool with multiple sign in options
  • How can i check the winner in the my Connect four java code? [closed]
  • difficulty using twitter api command implemention in python [duplicate]
  • AlarmManager and Notifications in Android 8.0(Oreo)
  • How to generate unique random numbers in an array
  • How to limit the display of questions in the quiz using json array in android?
  • Python--adding list into dict (beginner)
  • Random Number Generation without repetition in Java
  • Why are the outputs of this pseudo random number generator (LFSR) so predictable?
  • Passing string values from one form to another [duplicate]
  • Size of object serialized
  • How to select two additional columns from another table based on values in the main table?
  • How to display game on the screen?
  • How to generate two random numbers out of 4?
  • How to generate text files and download a zip file in Javascript?
  • Random Numbers in a range around a median
  • Generating Random Latin Square Continuous Loop
  • Java MongoDB getting value for sub document
  • What is a Stack Trace? [duplicate]
  • VBA Select Random Row In Range of Select
  • Playing multiple audio files using AVAudioPlayer
  • Mediaelements.js: override features attributes
  • Laravel Chat with Pivot Table how to return same users to same chat
  • Jump around in a video to times from an array
  • Select any random string from a list
  • Context is null in SignalR hub
  • Accessing the main thread in a TKinter script?
  • Yii: any way to save the images in compressed form?
  • How reduce the height of an mschart by breaking up the y-axis