我在将 CSS 样式应用于我的动态表格时遇到问题。我正在连接到数据库,我知道数据已正确填充并显示。我的问题是有 900 条记录,我想使用滚动条来限制表的大小。我读过 elseware,完成此任务的正确 CSS 样式节点是:How to specify table's height such that a vertical scroll bar appears?
溢出:自动;
高度:700px;
显示: block ;
溢出-y:滚动;
起初我尝试使用内联样式(我意识到这是不行的......),但它没有用。我已经阅读了有关向表和/或单个行添加“类”的信息,然后将其反射(reflect)在我的 CSS 样式表中,但我似乎无法弄清楚如何实现这一点。当我向 PHP 添加“span”或“class”标记指示符时出现语法错误(我想是使用“ECHO”——它们都需要双引号)。
我正在努力完成的一个很好的例子:http://www.timrivera.com/tests/csstables.html#markupIE
下面的 PHP 代码片段语法很好,但我不知道在哪里适本地添加类或跨度指示符。需要注意的一件事 - 我需要为不同的表格设置不同的样式,因此更改全局“表格”CSS 是行不通的。
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table>";
最佳答案
我认为最好的方法是在 table 周围包裹一个 div 并给 div 一个高度。
http://phpfiddle.org/main/code/i7h-9b1
<style type="text/css">
#scroll {
height: 100px; /* Max height of table */
overflow-y: scroll;
width: 340px;
}
</style>
<div id="scroll">
table
</div>
使用您的代码:
echo '
<style type="text/css">
#scroll {
height: 100px; /* Max height of table */
overflow-y: scroll;
width: 340px;
}
</style>';
//Function that gets the SQL recordset.
$result2 = Get_Package_Info_EXT($conn, $var_PartNumber);
//do the table edits here.
echo "<div id='scroll'><table border='1' >
<tr>
<th>Facility</th>
<th>Process Flow</th>
<th>Operation</th>
<th>Device</th>
<th>Item</th>
<th>Value</th>
<th>Database Source</th>
</tr>";
while($row2 = oci_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $row2['FACILITY_AT'] . "</td>";
echo "<td>" . $row2['SUB_FLOW_TYPE'] . "</td>";
echo "<td>" . $row2['OPN_NAME'] . "</td>";
echo "<td>" . $row2['SPEC_DEVICE'] . "</td>";
echo "<td>" . $row2['COMPONENT_NAME'] . "</td>";
echo "<td>" . $row2['COMPONENT_VALUE'] . "</td>";
echo "<td>" . $row2['SOURCE'] . "</td>";
echo "</tr>";
}
echo "</table></div>";
关于php - 使用 PHP 在动态创建表格中应用表格样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15573881/