2

I am trying to have the below CSS styling to be applied to the below table being fetched from mysql.

for example when I put

echo "<table id="result-table">";

right under the $result line it breaks the website.

<style> #result-table {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    border-collapse: collapse;
    background: #FFFFFF;
    width: 100%;
}

#result-table td,
#result-table th {
    width: 120px;
    padding-bottom: 10px;
    color: #2E3A7F;
    text-align: center;
}

#result-table th {
    width: 120px;
    padding-bottom: 10px;
    padding-top: 5px;
    text-align: center;
    background-color: #6F77A4;
    color: #FFFFFF;
}
</style>
<?php
  $servername = "localhost";
  $username = "Not User";
  $password = "Not Password";
  $dbname = "afoam";

  $conn = new mysqli($servername, $username, $password, $dbname);

  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }

  $sql = "SELECT * FROM emp";
  $result = $conn->query($sql);
  echo "<table>";
  echo "<tbody>";
  if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
      echo "<tr>";
        echo "<td>" . $row['eid'] . "</td>";
        echo "<td>" . $row['fna'] . "</td>";
        echo "<td>" . $row['lna'] . "</td>";
      echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";
  } else {
    echo "</tbody>";
    echo "</table>";
    echo "0 results";
  }
  $conn->close();
?>  

So how can I apply the styling to the table without an issue.

Just to recap I want to apply CSS styling to the Table that is being fetched from mysql.

But I get in error when I enter the CSS id for the styling in the html table brackets

Shadow
  • 33,525
  • 10
  • 51
  • 64
Isse Nur
  • 53
  • 8

1 Answers1

1

Change it to one of the following:

I would prefer:

echo '<table id="result-table">';

Or you can switch the quotes, that is a very little bit slower:

echo "<table id='result-table'>";

Or you can protect the inner quotes from being executed:

echo "<table id=\"result-table\">";
nito
  • 1,157
  • 8
  • 21