PHP Table: I am trying to create a new row for each row of the mysql table on my website

Basically, I got data from mysql on 1 row and made it look like a table with borders, etc., but I would like it to be repeated for every row in the mysql table. Here is the following code that I have used so far:

while($info = mysql_fetch_array( $data )) { echo "<table width='1000' cellpadding='10' cellspacing='5' border='1'>"; echo "<tr> <td><center><b>".$info['id']."</td></center></b>"."<td><center><b>".$info['username']."</td></center></b>"."<td><center><b>".$info['kills']."</td></center></b>"."<td><center><b>".$info['deaths']."</td></center></b>"."<td><center><b>".$info['ratio']."</td></center></b></tr>"; echo "</table>"; } 

UPDATE:

  echo "<table width='1000' cellpadding='10' cellspacing='5' border='1'>"; while($info = mysql_fetch_array( $data )) { echo "<tr> <td><center><b>".$info['id']."</td></center></b>"."<td><center><b>".$info['username']."</td></center></b>"."<td><center><b>".$info['kills']."</td></center></b>"."<td><center><b>".$info['deaths']."</td></center></b>"."<td><center><b>".$info['ratio']."</td></center></b></tr>"; } echo "</table>"; 
+4
source share
2 answers

You want the tage table to receive data rows outside the loop:

 echo "<table width='1000' cellpadding='10' cellspacing='5' border='1'>"; while($info = mysql_fetch_array( $data )) { echo "<tr> <td><center><b>".$info['id']."</td></center></b>"."<td><center><b>".$info['username']."</td></center></b>"."<td><center><b>".$info['kills']."</td></center></b>"."<td><center><b>".$info['deaths']."</td></center></b>"."<td><center><b>".$info['ratio']."</td></center></b></tr>"; } echo "</table>"; 

Since you're new to PHP, I should probably expand this a bit more:

Your code will now display the table open tag:

 <table width='1000' cellpadding='10' cellspacing='5' border='1'> 

Then go to the while loop and highlight all the lines found:

 <tr><td><center><b>Something1></b></td></tr> // shortened for example. <tr><td><center><b>Something2></b></td></tr> <tr><td><center><b>Something3></b></td></tr> <tr><td><center><b>Something4></b></td></tr> 

Then, when it exists in the loop, it will output a closing tag for the table:

 </table> 

What you did earlier is the output of the table for each row of data. For future reference, it is recommended that you study the source code to understand what is happening.

Edit: When I looked at your comment on the code, I formatted it more carefully to find out what is happening, you can make your code more readable in the following format:

 echo "<table width='1000' cellpadding='10' cellspacing='5' border='1'>"; while($info = mysql_fetch_array( $data )) { echo "<tr> <td><center><b>".$info['id']."</b></center></td> <td><center><b>".$info['userโ€Œโ€‹name']."</b></center></td> <td><center><b>".$info['kills']."</b></center></td> <td><center><b>".$info['deaths']."</b></center></td> <td><center><b>".$info['โ€Œโ€‹ratio']."</b></center></td> </tr>"; } echo "</table>"; 
+5
source

Just move the table tags from the loop.

 echo '<table>'; foreach( $row ) { echo '<tr>....</tr>'; } echo '</table>'; 
+1
source

Source: https://habr.com/ru/post/1433080/


All Articles