TimTims
TimTims

Reputation: 43

PHP/HTML - Table Not Displaying Correctly

Currently I have a table that's linked to a MySQL Database and it get's the rows from the database.

It works perfectly and the arrays are displayed fine.

But instead of a normal table showing it down one each time, it's showing it side by side like below.

Table Display

The Player Names are blanked out due to privacy. But the blanked out spaces are where the Player is.

Here is the database connect file:

<?php
// MySQL Connection Details
$mysql_host = ""; // Host name 
$mysql_username = ""; // Mysql username 
$mysql_password = ""; // Mysql password 
$mysql_database = ""; // Database name 
$mysql_table = ""; // Table name 

// MySQL Connection
$con = mysqli_connect("$mysql_host","$mysql_username","$mysql_password","$mysql_database");

// MySQL Error Logging
if (mysqli_connect_errno())
  {
  echo "MySQL Connection Failed: " . mysqli_connect_error();
  }

$sql = "SELECT * FROM $mysql_table";
$result = $con->query($sql);  

?>

Below is how it's displayed in the main php file.

<table id="ldr_table">
    <tr class="lb_tb_hd" align="center">
        <td>Player</td>
        <td>Points</td>
        <td>Wins</td>
        <td>Kills</td>
        <td>Deaths</td>
        <td>Played</td>
    </tr>
    <tr class="ldr_alt">
        <?php
        require('{db connection file}');
        while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
        {   
        echo '<td><center>' . $row['player_name'] . '</center></td>';
        echo '<td><center>' . $row['score'] . '</center></td>';
        echo '<td><center>' . $row['games_won'] . '</center></td>';
        echo '<td><center>' . $row['kills'] . '</center></td>';
        echo '<td><center>' . $row['deaths'] . '</center></td>';
        echo '<td><center>' . $row['games_played'] . '</center></td>';
        }
        mysqli_close($con);
        ?>
    </tr>
</table>

I've tried several things including foreach which had no effect on the format.

If there are other files needed I'll happily show them here.

Upvotes: 0

Views: 1779

Answers (3)

Domain
Domain

Reputation: 11808

.Place <tr> tag within while loop

<table id="ldr_table">
        <tr class="lb_tb_hd" align="center">
            <td>Player</td>
            <td>Points</td>
            <td>Wins</td>
            <td>Kills</td>
            <td>Deaths</td>
            <td>Played</td>
        </tr>

            <?php
            require('{db connection file}');
            while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
            {   
            echo '<tr class="ldr_alt">'
            echo '<td><center>' . $row['player_name'] . '</center></td>';
            echo '<td><center>' . $row['score'] . '</center></td>';
            echo '<td><center>' . $row['games_won'] . '</center></td>';
            echo '<td><center>' . $row['kills'] . '</center></td>';
            echo '<td><center>' . $row['deaths'] . '</center></td>';
            echo '<td><center>' . $row['games_played'] . '</center></td>';
            echo '</tr>'
            }
            mysqli_close($con);
            ?>

    </table>

Upvotes: 0

Orelsanpls
Orelsanpls

Reputation: 23565

You need to create a new row for each SQL result

<?php
require('{db connection file}');
while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
     echo '<tr class="ldr_alt">';
     echo '<td><center>' . $row['player_name'] . '</center></td>';
     echo '<td><center>' . $row['score'] . '</center></td>';
     echo '<td><center>' . $row['games_won'] . '</center></td>';
     echo '<td><center>' . $row['kills'] . '</center></td>';
     echo '<td><center>' . $row['deaths'] . '</center></td>';
     echo '<td><center>' . $row['games_played'] . '</center></td>';
     echo '</tr>';
}
mysqli_close($con);

?>

PS: use

text-align: center;

Instead of multiple center balise.

Upvotes: 1

Ritesh
Ritesh

Reputation: 362

Echo <tr> tag inside while loop.

while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
    echo '<tr class="ldr_alt">';
    echo '<td><center>' . $row['player_name'] . '</center></td>';
    echo '<td><center>' . $row['score'] . '</center></td>';
    echo '<td><center>' . $row['games_won'] . '</center></td>';
    echo '<td><center>' . $row['kills'] . '</center></td>';
    echo '<td><center>' . $row['deaths'] . '</center></td>';
    echo '<td><center>' . $row['games_played'] . '</center></td>';
    echo '</tr>';
}

Upvotes: 0

Related Questions