我有一個關於使用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