猿问

如何在php中将此css样式应用于此表

我正在尝试将下面的CSS样式应用于从mysql获取的下表。


例如,当我把


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

就在$result线下面,它打破了网站。


<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();

?>  

那么我如何将样式应用于表格而不会出现问题。


只是为了回顾一下,我想将CSS样式应用于从mysql获取的表格。


但是当我在html表括号中输入样式的CSS ID时,我遇到了错误


杨魅力
浏览 97回答 1
1回答

ibeautiful

将其更改为以下之一:我更喜欢:echo&nbsp;'<table&nbsp;id="result-table">';或者你可以切换报价,这有点慢:echo&nbsp;"<table&nbsp;id='result-table'>";或者,您可以保护内部报价不被执行:echo&nbsp;"<table&nbsp;id=\"result-table\">";
随时随地看视频慕课网APP
我要回答