用于创建,填充和显示html表的PHP工具

时间:2013-04-07 12:46:59

标签: php html-table

我有一个关于使用PHP创建HTML表的问题。我喜欢一些库处理SQL创建,读取,更新和删除(CRUD)的方式,使用可以执行CRUD的PHP组件,而不需要知道任何SQL,而是使用PHP API。

我需要一个工具,我可以用同样的方式创建HTML表格。我想仅使用PHP语句创建HTML或其他ML表。

有人能建议用PHP创建HTML表的好工具吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

确实有这样的工具可以使用PHP开发HTML表单。

我作为PHP开发人员的首选是PEAR的HTML_Table。正如文档所说“[PEAR] HTML_Table使得HTML表的设计变得简单,灵活,可重用和高效。”

使用这个组件很容易,包括表类(来自文件),实例化一个新实例,添加一个正文并开始使用PHP调用向表中追加行。

以下是显示用户姓名,电子邮件和年龄的表格示例。

此示例假设您已安装PEARInstall PEAR)以及PEAR HTML_Table

首先要做的是包含PEAR的HTML_Table

<?php require_once 'path/to/pear/HTML/Table.php'; ?>

您可能还需要添加HTML_Common&amp; PEAR类也是如此,因此建议在PHP include_path中使用PEAR路径。

要处理这个问题并且通常使用PEAR类加载,请查看PSR-0标准,它是类和文件的PEAR命名约定。在使用自动加载器时,这可能很有用。

让类可用,我们可以创建一个这样的表:

// 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' => 'john.doe@example.com',
        'age'   => 33,
    ),
    array(
        'name'  => 'Jane Doe',
        'email' => 'j.doe@example.com',
        '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>john.doe@example.com</td>
            <td>33</td>
        </tr>
        <tr>
            <td>Jane Doe</td>
            <td>j.doe@example.com</td>
            <td>30</td>
        </tr>
    </tbody>
</table>

希望这会有所帮助:)

斯托。

相关问题