How to add a table header row(s)

One frequently asked question is how to add header rows to a table that is produced by a database query.
for instance, let's assume that you want to display a list of articles with the following query:

SELECT `name`,`username`,`email` FROM `#__users` WHERE 1
The output is something similar to:

John Doe jdoe doe@gmail.com
Mary Poppins marry mpoppin@hotmail.com
Herny Ford hernyford herny@ford.com

This looks good, but you want to add a top header to describe each row. So, the ideal output would be:

Full Name Username Email
John Doe jdoe doe@gmail.com
Mary Poppins marry mpoppin@hotmail.com
Herny Ford hernyford herny@ford.com

There are two ways to achieve that:

1. The easiest and the proper way is to add the header information in the database query, similar to:

(SELECT "Full Name","Username","Email") UNION (SELECT `name`,`username`,`email` FROM `#__users` WHERE 1)

2. If you want to do something more complicated, let's say you want to add conditional table headers, you can use a modification function.
This is basically a PHP function that takes as input the table data and returns a modifed version of them, before any other rule is applied (unless of course the rule priority has been set). As  a reference point, the Tabulizer version 5.0 contains a modification function named addrow, so you could achieve the same result by adding a table rule with the following settings:

  • Function name: addrow
  • Function args: 1:,:Full Name,Username,Email

The function args require some explanation.

  • 1 : The position where the row will be added. "1" means add it as top row
  • , : This is the column delimiter (comma is this example). This can be any string, not necessarily a single character.
  • Full Name,Username,Email: These are the column data, separated by the column delimiter.

So, in general the following 3 argument parts are expected (separated by the colon):
row id:column separator:column data