我在 HTML 文件中有以下代码。这将创建一个样式列表。
<style>
/* LIST #8 */
#list8 { }
#list8 ul { list-style:none; }
#list8 ul li { font-family:Georgia,serif,Times; font-size:18px; }
#list8 ul li a { display:block; width:300px; height:28px; background-color:#333; border-left:5px solid #222; border-right:5px solid #222; padding-left:10px;
text-decoration:none; color:#bfe1f1; }
#list8 ul li a:hover { -moz-transform:rotate(-5deg); -moz-box-shadow:10px 10px 20px #000000;
-webkit-transform:rotate(-5deg); -webkit-box-shadow:10px 10px 20px #000000;
transform:rotate(-5deg); box-shadow:10px 10px 20px #000000; }
</style>
<div id="list8">
<ul>
<li><a href="#">9th Fall 2017</a></li>
<li><a href="#">10th Spring 2018</a></li>
</ul>
</div>
这是 HTML 文件呈现的方式。
我有以下 PHP 文件。
<?php
$link = mysqli_connect("localhost", "root", "password", "Booking_Databse");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT SemesterName FROM Semester_tbl";
if ($result = mysqli_query($link, $query)) {
?>
<style>
/* LIST #8 */
#list8 { }
#list8 ul { list-style:none; }
#list8 ul li { font-family:Georgia,serif,Times; font-size:18px; }
#list8 ul li a { display:block; width:300px; height:28px; background-color:#333; border-left:5px solid #222; border-right:5px solid #222; padding-left:10px;
text-decoration:none; color:#bfe1f1; }
#list8 ul li a:hover { -moz-transform:rotate(-5deg); -moz-box-shadow:10px 10px 20px #000000;
-webkit-transform:rotate(-5deg); -webkit-box-shadow:10px 10px 20px #000000;
transform:rotate(-5deg); box-shadow:10px 10px 20px #000000; }
</style>
<div id="list8">
<ul>
</ul><?php
/* fetch associative array */
while ($row = mysqli_fetch_row($result)) {
echo '<li><a href="#">' . $row[0] . '</a></li>';
}
?>
</ul>
</div>
<?php
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
PHP 文件是这样呈现的:
我希望 PHP 文件像 HTML 文件一样呈现。我不知道我做错了什么。
最佳答案
您关闭了您的 <ul>
在做循环之前...
参见:
<div id="list8">
<ul>
</ul><?php
/* fetch associative array */
while ($row = mysqli_fetch_row($result)) {
echo '<li><a href="#">' . $row[0] . '</a></li>';
}
?>
</ul>
</div>
更改为:
<div id="list8">
<ul>
<?php
/* fetch associative array */
while ($row = mysqli_fetch_row($result)) {
echo '<li><a href="#">' . $row[0] . '</a></li>';
}
?>
</ul>
</div>
关于php - 静态页面有效,但是当放入 PHP 时,CSS 无法正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47966099/