{array}
Runs a SQL query on the application database and repeats its content once per row.
Syntax
{array "* from table where ..."} ... {/array}
{array "* from orders", name='orders'} ... {/array}
Parameters
-
sql -
The query after the word SELECT -
"* from users order by name".
Options
-
name -
Names the level, so its properties read
first@orders; in the designer preview (&padSample) the sample's rows of that name answer instead of the database.
Description
{array "..."} runs its parameter as a query and makes every row it answers an occurrence of the tag: the content renders once per row, with the row's columns as fields - {$name}, {$email}. It is the template form of db ( "ARRAY ..." ); the tag name is the command word, so the parameter starts after the SELECT.
A query that answers no rows renders nothing, or the @else@ branch when the content has one. Every handling option works on the rows as on any data - first=, sort=, where=, page= with a {pager} after it - and the loop properties are there: {first@array}, {current@array}. Sorting and limiting are usually cheaper in the SQL itself.
Most pages fetch their rows in PHP instead - $orders = db ( "ARRAY * FROM orders" ); and then {orders}...{/orders} - which keeps SQL out of the template and lets a request value go in through a placeholder. {array} is handy for a quick listing. With name= the rows can come from the page's sample in the designer preview.
Examples
A row per occurrence
The content renders once per row, the columns as fields.
<ul>
{array "name, city from customers order by name"}
<li>{$name} - {$city}</li>
{/array}
</ul>
Options and an empty result
Handling options work on the rows; @else@ shows when there are none.
{array "number, total from orders where status='open'", name='open', sort='total DESC', first=5}
{first@open}<table>{/first@open}
<tr><td>{$number}</td><td>{$total | currency('EUR')}</td></tr>
{last@open}</table>{/last@open}
@else@
<p>No open orders.</p>
{/array}
See also
- {record} Runs a SQL query on the application database and renders its content with the first row's columns as fields.
- {field} Runs a SQL query on the application database and prints the single value it answers.
- {check} Asks the application database whether a row exists, and renders its content only when one does.
- {pager} Page links for a tag written with the page option: first and last, a window round the current page, previous and next.