0

I want my table centered so I tried this: <table align=center>HEEHHE</table> which doesn't work.

My code:

<?php
$servername = "***:3306";
$username = "**";
$password = "!!!!";
$dbname = "***";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT balance FROM tbl_users WHERE userID = 8";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table align="center"><tr><th>Aktuell im Jackpot:</tr></th>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["balance"]."</tr></td>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>
Jonathon Ogden
  • 1,562
  • 13
  • 19

3 Answers3

1

align="center" will probably still work, even though it's a deprecated attribute (see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table). You should probably use CSS for styling, e.g. table { margin: 0 auto; } should do it.

You have multiple issues in your code though:

  • the tr is closed before the th element (see below for the right structure)
  • the td is closed before the th element (see below)
  • double quote syntax error (as hinted by Audite Marlow)

table {
  margin: 0 auto;
  }
<table>
  <tr><th>Aktuell im Jackpot:</th></tr>
<tr><td>1231234</td></tr>
</table>
MattDiMu
  • 4,873
  • 1
  • 19
  • 29
0

This should do it:

<table height="100%" width="100%">
<tr>
<td align="center" valign="middle">
<p>Content</p>
</td>
</tr>
</table>

There are some error's:

echo "<table align="center"><tr><th>Aktuell im Jackpot:**</th></tr>**";
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["balance"]."**</td></tr>**";
}
echo "</table>";
0

First of all, your code is wrong:

echo "<table align="center"><tr><th>Aktuell im Jackpot:</tr></th>"; // wrong code

You should escape quotation marks: \"center\"; Or use single quotes like echo '<table style="margin: 0 auto;">...</table>';.

Well, it has a better way you stylize things, but I'm going straight to the point: instead align="center", use style="margin: 0 auto; width: 500px;".

If you need to center in vertical, this guide is good for you: https://css-tricks.com/centering-css-complete-guide/

Lucas Martins
  • 508
  • 6
  • 9