table($data, $params) table()
Start a table: rows from an array, an SQL query, a query builder or a callback, then one call per column, button or link.
table()->chained_wrapper($sql = '', $params = [])
Options
| Option | Meaning |
|---|
$data: array | Rows keyed by id ([id => [field => value]]); filtered, sorted and paged in PHP. |
$data: string | An SQL SELECT; table2 adds the filter WHERE, the ORDER BY and the LIMIT of the page, and runs it on db() (or $params['db']). |
$data: query builder | from('shop_products')->where(...) is turned into its SQL first. |
$data: callable | Called with the params, returns the rows array. |
$params | Table options: see render(). Options passed to render() later override these. |
Example
| Name |
Price |
| Desk lamp |
24.90 |
| Office chair |
149.00 |
From an array
| Name |
Price |
| Office chair |
149.00 |
| Desk lamp |
24.90 |
From a callback
Code
$rows = [
7 => ['id' => 7, 'name' => 'Desk lamp', 'price' => '24.90'],
8 => ['id' => 8, 'name' => 'Office chair', 'price' => '149.00'],
];
$from_array = table($rows, ['no_pages' => 1, 'caption' => 'From an array'])
->text('name')
->text('price');
$from_callback = table(fn ($params) => array_reverse($rows, true), ['no_pages' => 1, 'caption' => 'From a callback'])
->text('name')
->text('price');
return $from_array . $from_callback;
Notes
table() (alias table2()) clones the table2 object and calls chained_wrapper($data, $params): every call gets a fresh table, the CLASS_* and ESCAPE_* settings of the site stay.- Each column method returns the table, so calls chain; the table renders when it is printed (
__toString()) or when render() is called. - An SQL string is site code, never request data: the filter and the htmx sort add their conditions from whitelisted names only.
See also: table/render, form/form