【发布时间】:2016-01-31 21:31:35
【问题描述】:
我有一个从数据库返回正确结果的查询,我想用结果填充 HTML 表。但是,我只显示“原始”数据,表格没有按照 CSS 类显示。
我的问题:我的表格类标签放置在正确的位置吗?我认为这是某个地方的错误。
<table class="table-class">
<thead>
<tr><th>Car</th><th>Year</th><th>HP</th><th>Seats</th></tr>
</thead>
<tbody>
<?php
while($row = $resultsql->fetch_assoc()){
?>
<tr>
<td>
<?php echo $row['Car']; ?>
</td>
<td>
<?php echo $row['Year']; ?>
</td>
<td>
<?php echo $row['HP']; ?>
</td>
<td>
<?php echo $row['Seats']; ?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
我的 CSS:
.table-class {
margin: 1em 0;
width: 100%;
}
@media (min-width: 480px) {
.table-class {
width: auto;
}
}
.table-class tr {
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
}
.table-class tr td {
text-align: left;
}
@media (min-width: 480px) {
.table-class tr td {
text-align: center;
}
}
.table-class tr td:first-of-type {
text-align: left;
}
.table-class th {
display: none;
}
.table-class td {
display: block;
}
.table-class td:first-child {
padding-top: .5em;
}
.table-class td:last-child {
padding-bottom: .5em;
}
.table-class td:before {
content: attr(data-th) ": ";
font-weight: bold;
width: 9em;
display: inline-block;
}
@media (min-width: 480px) {
.table-class td:before {
display: none;
}
}
.table-class th:first-of-type {
text-align: left;
}
.table-class th, .table-class td {
text-align: center;
}
@media (min-width: 480px) {
.table-class th, .table-class td {
display: table-cell;
padding: .25em .5em;
}
.table-class th:first-child, .table-class td:first-child {
padding-left: 0;
}
.table-class th:last-child, .table-class td:last-child {
padding-right: 0;
}
}
【问题讨论】:
-
能否在您的问题中添加 CSS 代码?任何包含
.table-class的内容都应包括在内。 -
我对 PHP 不熟悉,但您不会缺少
<tbody>…</tbody>标记吗?<table class="table-class">是对的,顺便说一句。 -
嗯,我想我明白了:while 循环缺少结束
}。 -
@PerlDog 没有做到这一点,但谢谢,错过了。 tbody 标签去哪了?
-
@FelixF。这完全改变了事情。我以为您在浏览器中只看到垃圾或 php 代码或其他内容。您是否已将 CSS 包含到您的页面中?没有错别字?而不是
<table class=...>试试<table style="color:blue;">看看它是否是蓝色的。