How to Add a Header Row to a Database Table/Query Data Source

How to Add a Header Row to a Database Table/Query Data Source

A database query returns values, not column names, so a table built from one starts without headings. That is deliberate: Tabulizer does not force cust_name or COUNT(*) on your visitors. You choose the header row yourself, and there are three ways to do it.

The short answer

1

The data source option

Tick Add a header row when results are not empty and type one label per column. The recommended way.

2

A UNION in the query

Put a row of labels in front of the data in SQL. Works, but it gets in the way of sorting and some servers block it.

3

An Add Row rule

Add the header in the ruleset, in step 5 of the online Table Wizard. Useful when one ruleset serves several queries.

Whichever you pick, the labels become row 1 of the table. To have them styled as headings, use a ruleset with 1 top header - in step 2 (Format) of the Table Wizard.

Joomla or WordPress? All three work the same way on both. The data source form has the same fields and the same header row editor in the Joomla administrator and in WordPress, so the Joomla 5 screenshot below stands for both. Only the way to the form differs: Components › Tabulizer › Data Sources in Joomla, Tabulizer › Data Sources in the WordPress dashboard.

Way 1: the data source's own header row

Open the data source - or create one with New Data Source - and set Source Type to Database Table/Query.

1

Tick the box

Under the query, tick Add header row - its label reads Add a header row when results are not empty. A Header row editor appears beneath it.

2

Add one label per column

Press + Add header column once for every column your query returns and type the heading in each box.

3

Put them in order

Drag the labels by their handle until they follow the order of your SELECT list. Remove deletes one. Then save the data source.

A Database Table/Query data source in the Joomla administrator: the query SELECT name, email, city FROM customers ORDER BY name, the Add header row box ticked, and three header labels - Customer Name, Customer Email and City - each with a drag handle and a Remove button, above the Add header column button.

For the query above, name, email and city arrive under the headings Customer Name, Customer Email and City.

The header row is added only when the query returns rows. An empty result never turns into a table of headings alone - your Empty results message is shown instead.

The same option is offered by the External Database/Query and Form Entries data sources.

Way 2: a UNION in the query

Before the option above existed, the usual trick was to select a row of fixed text and glue the data underneath it:

(SELECT 'Customer Name', 'Customer Email')
UNION ALL
(SELECT name, email FROM customers)

Use UNION ALL rather than plain UNION: UNION also throws away duplicate rows, which is rarely what you want in a table.

Two reasons to prefer Way 1.
  • Sorting. A database does not promise to return the label row first, and an ORDER BY inside the second part is ignored unless it comes with a LIMIT. If you need the rows sorted before they reach Tabulizer, the labels can end up somewhere in the middle.
  • Security filters. Many web servers run a firewall (ModSecurity and similar) that treats UNION SELECT in a submitted form as an SQL injection attempt. On such a server, saving the data source fails with a 403 or a blank page, through no fault of the query.

Way 3: an Add Row rule in the ruleset

The header can also belong to the ruleset rather than the data source. Open the online Table Wizard, go to step 5 (Modifications) and, under Add/Remove Elements, press Add new Data Addition:

1

Choose a custom row

Leave Table Element at Table and Element Range at all, and pick Custom Row (Type your values).

2

Put it first

Set Insert Row Position to 1st.

3

Type the labels

The wizard shows one box per column of your table. Type a heading in each.

A Data Addition rule in step 5 of the online Table Wizard: Table Element Table, Element Range all, Custom Row selected, Insert Row Position 1st, and the values Customer Name, Customer Email and City typed for columns 1 to 3.

With 1 top header set in step 2, the added row is drawn as the table's heading row. Import the ruleset into your site and assign it to the data source.

The number of boxes follows the column count you gave in step 1, so load sample data with as many columns as your query returns.

Which one should I use?

Way Choose it when
Data source option Almost always. The labels live next to the query they describe, the order of the rows is untouched, and nothing in it looks suspicious to a firewall.
UNION You are maintaining an older data source that already does it this way, and it works.
Add Row rule You want the same headings on several data sources that share one ruleset, or you build the table with the wizard anyway.

Common questions

The labels show as an ordinary row, not as headings.

The ruleset has no header. Pick one with 1 top header, or set it in step 2 of the Table Wizard.

The headings are over the wrong columns.

They follow the order you gave them, not the column names. Drag them - or reorder your SELECT list - until the two match.

I used two of these ways and now have two header rows.

Each adds its own row. Keep one.

Saving the data source gives a 403 error.

Your server's firewall is probably blocking the UNION. Remove it and use the Add header row option instead.

Read more

The full reference for this data source is The Database Table / Query Data Source, and every kind of Data Addition is covered in Add Rows and Columns.

More answers

Browse the rest of the Frequently Asked Questions, or open the Table Wizard to build a ruleset with a header.

Back to the FAQ