php - 使用 PHP 在动态创建表格中应用表格样式

标签 php html css forms

我在将 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/

相关文章:

php - mySQL 查询不存在

html - 为什么我的 Logo 和导航栏之间存在巨大的可点击间隙以及如何消除它?

php - 为什么 PHP PDO 在 mysql 命令行工作时得到 "SQLSTATE[42000] [1044] Access denied for user"?

php - php 扩展崩溃 - 将内存地址转换为行号

html - 嵌套列/行

html - Twitter Bootstrap : pull-right class only working in Firefox for input-group

javascript - 显示 Bootstrap 下拉菜单以将列表定向到左侧而不是右侧

IOS 上的 Chrome 41 忽略了用于打印的 Css

php - json_encode 来自数据库的数据数组

Javascript 图像幻灯片放映循环