0

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.

2
  • 1
    Shouldnt your tr be instead your while so you get a able row with all your data for a retruend row, then close the table row, then go throug hthe while loop again ? Commented Aug 5, 2014 at 19:17
  • You don't need to put quote marks around your variables, such as $mysql_host, unless you intend to add text with it. Commented Aug 5, 2014 at 19:23

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>';
}

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.