我有一个关于使用PHP创建HTML表格的问题。我喜欢某些库通过使用可以执行CRUD的PHP组件来处理SQL创建,读取,更新和删除(CRUD)的方式,而不需要知道任何SQL,而是使用PHP API。用于创建,填充和显示html表格的PHP工具
我需要一个工具,我可以用同样的方法创建HTML表格。我只想使用PHP语句创建HTML或其他ML表。
任何人都可以提出一个很好的工具来使用PHP创建HTML表格吗?
在此先感谢。
我有一个关于使用PHP创建HTML表格的问题。我喜欢某些库通过使用可以执行CRUD的PHP组件来处理SQL创建,读取,更新和删除(CRUD)的方式,而不需要知道任何SQL,而是使用PHP API。用于创建,填充和显示html表格的PHP工具
我需要一个工具,我可以用同样的方法创建HTML表格。我只想使用PHP语句创建HTML或其他ML表。
任何人都可以提出一个很好的工具来使用PHP创建HTML表格吗?
在此先感谢。
确实有使用PHP开发HTML表单的工具。
我作为一名PHP开发人员的首选是PEAR的HTML_Table
。正如文档所言:“[PEAR's] HTML_Table使HTML表格的设计变得简单,灵活,可重用和高效。”
使用这个组件很容易,包括表类(从文件),实例化一个新的实例,添加一个主体并开始使用PHP调用将表追加到表中。
下面是一个显示用户姓名,电子邮件和年龄的表格示例。
本示例假定您已安装PEAR
(Install PEAR)以及PEAR的HTML_Table。
要做的第一件事就是包括PEAR的HTML_Table
<?php require_once 'path/to/pear/HTML/Table.php'; ?>
您可能还需要包括HTML_Common
& PEAR
类以及因此它是很好的建议,在你的PHP include_path
PEAR的路径。
要解决这个问题,并且一般用PEAR类加载,请看PSR-0标准,它是类和文件的PEAR命名约定。这在使用自动加载器时可能会很有用。
具有类(ES)可用,我们可以创建一个表是这样的:
// Instantiating the table. The first argument is the HTML Attributes of the table element
$table = new HTML_Table(array('class' => 'my-table'), null, true);
注意,所有的参数都是可选的。 让我们首先添加页眉表:
// Preparing the header array this will go in <table><thead><tr>[HERE..]</tr></thead></table>
$headerRow = array('Name', 'Email', 'Age');
$header = $table->getHeader();
$header->setAttributes(array('class' => 'header-row')); // sets HTML Attributes of the <thead /> element
$header->addRow($headerRow, null ,'th');
到目前为止,这个表的HTML看起来像这样:
<table class="my-table">
<thead class="header-row">
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</tr>
</thead>
</table>
让我们添加一个体和一些行:
// This is array of arrays that will represent the content added to the table as table rows (probably retrieved from DB)
$resultSet = array(
array(
'name' => 'John Doe',
'email' => '[email protected]',
'age' => 33,
),
array(
'name' => 'Jane Doe',
'email' => '[email protected]',
'age' => 30,
),
);
// $bodyId is the body identifier used when appending rows to this particular body
$bodyId = $table->addBody(array('class' => 'main-body'));
foreach ($resultSet as $entry) {
$indexResult = array_values($entry); // <-- the array passed to the addRow must be indexed
$table->addRow($indexResult, array (/* attributes */), 'td', true, $bodyId);
// note how we specify the body ID to which we append rows -----------^
// This is useful when working with multiple table bodies (<tbody />).
}
的表格中的多个<tbody />
标签的概念也可以利用表类的addBody()
方法,该方法返回身体标识符为在稍后添加行时用作参考(请参阅上面的评论)。
有了这样的,显示表一样简单:
<?php
echo $table->toHtml();
// or simply
echo $table;
?>
这个例子中的HTML内容现在看起来是这样的:
<table class="my-table">
<thead class="header-row">
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</tr>
</thead>
<tbody class="main-body">
<tr>
<td>John Doe</td>
<td>[email protected]</td>
<td>33</td>
</tr>
<tr>
<td>Jane Doe</td>
<td>[email protected]</td>
<td>30</td>
</tr>
</tbody>
</table>
希望这有助于:)
斯托扬。
为什么你想用php创建html表格? – epicdev 2013-04-07 12:48:30
那么,一个建议是我可以准备一个表的属性,样式等,然后将结果集注入它,这将填充表。另一个是我可以(可能)自动实现旧的html和html5。 – 2013-04-07 12:50:55