GENWiki

Premier IT Outsourcing and Support Services within the UK

User Tools

Site Tools


php:using_the_gen_table_data_framework

The GEN Table Data Framework (TDF)

There are so many times in PHP where you need to take a $row array and show it as a table, and its not that hard to do, but the repetitive echo "<td>".$row["FIELD"]."</td> gets a little tiresome especially if each field has to have a class, and a style etc and that's before you start adding running totals.

The GEN TDF is a set of functions that make the whole process easier to do in most cases.

First, you define the table, what's in it and how its displayed and this done in an array.

$table=array("FIELD"=>array("h"=>"heading", "f"=>"", "rs"=>"text-align:left;"));

So in essence, an array of fields from the data row, each with a sub-array of parameters. The following parameters are available to set for each column:

  • h = Heading to be shown in the row header. A heading of |n where N is a single digit number places a spacer in the column.
  • rc = row class. This can be changed in the loop code, e.g. $table["FIELD"]["rc"⇒"color:blue;"]
  • rf = row format, number of decimals, can be 0 or 2
  • rs = row style. Can also be changed in the loop.
  • prf = prefix, shown before the data.
  • pof = postfix, shown after the data. e.g. $table["TEMP"]=array("h"⇒"Temperature", "pof"⇒"c")
  • link = Add a html a element around the value linking to what's provided here.
  • html = wrap value in html given, replace with field value.

Here's an example:

$table=array(
	"AGENT"=>array("h"=>"AGENT", "f"=>"", "rs"=>"text-align:left;"),
	"|1"=>array("hs"=>"background:black;"), 				
	"JAN"=>array("h"=>"Jan", "rs"=>"text-align:center"), 
	"FEB"=>array("h"=>"Feb", "rs"=>"text-align:center"), 
	"MAR"=>array("h"=>"Mar", "rs"=>"text-align:center"), 
	"APR"=>array("h"=>"Apr", "rs"=>"text-align:center"), 
	"MAY"=>array("h"=>"May", "rs"=>"text-align:center"), 
	"JUN"=>array("h"=>"Jun", "rs"=>"text-align:center"), 
	"JUL"=>array("h"=>"Jul", "rs"=>"text-align:center"), 
	"AUG"=>array("h"=>"Aug", "rs"=>"text-align:center"), 
	"SEP"=>array("h"=>"Sep", "rs"=>"text-align:center"), 
	"OCT"=>array("h"=>"Oct", "rs"=>"text-align:center"), 
	"NOV"=>array("h"=>"Nov", "rs"=>"text-align:center"), 
	"DEC"=>array("h"=>"Dec", "rs"=>"text-align:center"), 
	"|2"=>array("hs"=>"background:black;"), 				
	"SALES"=>array("h"=>"Sales", "rs"=>"text-align:center","rf"=>"2", "pof"=>"%")
	);

Inspecting the last line we break this down to:

Field is called SALES from the $row data we get from our database. The Heading for that column is going to be Sales, and the style will be text-align centre. 2 decimal places, and a % appended. Assuming the value of SALES was 25, you would see 25.00% centred in the cell.

After defining the table layout, and after outputting the <Table> HTML you build the header thus:

echo buildTableHeader($table,"Grad3","text-align:center;");

Here we have the table definion in $table, the Class Grad3 and the style.

Now within a loop, call

echo buildTableRow($table, $row, $rowclass, "GREY5");

Again here we see the table definion $table, the actual row as an associated array $row, the rowclass and dataclass, being the <tr> class and <td> class respectively.

If you need a seperator row, this can be generated using

echo buildBlankTableRow($table,"thinorange");

Giving the table definition and the row class, in this case thinorange.

DYNAMIC

Within our loop we sometimes want to dynamically change the contents of a table cell per record. We can do this by simply changing the $table (or whatever you called the array) within the loop. An example would be:

$table["CLI"]=array("h"=>"CLI", "html"=>"<input type=\"submit\" name=\"cli\" value=\"%%%%\">");

in which case for the field CLI, we're wrapping the contents within an input tag. is replaced with whatever the field content is.

LINKING

Sometimes you may want a particular value to be linked to a destination using the <a> html element. You can do this using "html" as above, or for a simply solution you can use "link" for example:

$table["CLI"]=array("h"=>"CLI", "link"=>"somepage.php?cli=%%%%&something=yes");

Again, in this instance, is replaced by the actual field value.

TOTALS

Totalling columns is trivial but made easier using this framework. First define an array to hold the totals, e.g.

$totals=array("RECORDS"=>0, "P1"=>0, "P2"=>0, "MAY"=>0, "P3"=>0, "P4"=>0, "P5"=>0);

The field names match the row and table definitions of course.

Now we can add to the totals array with this

addTotals($totals,$row);

This takes the values from the $row that match the $totals and adds them. Since this is all done with pointers, we can manually hack about the $totals after we've called addTotals such as:

addTotals($totals,$row);
$totals["SOMEFIELD"].-$row["ANOTHERFIELD"];

We can output the totals in the same way as any row:

echo buildTableRow($table, $totals, "Grad2", "highlight");

Which builds the HTML table row and output's it.

EXAMPLE

$data is an associative array of an entire table that we've just pulled from the database. For each Record we have NAME, AGE AND DOB.

$table=array("NAME"=>array("h"=>"Name"), "AGE"=>array("h"=>"Age"), "DOB"=>array("h"=>"Date of birth"));
echo "<table width=\"100%\">";
echo buildTableHeader($table,"ClassBlue","text-align:center;");
foreach($data as $row) {
   echo buildTableRow($table, $row, "ClassGrey", "GREY5");
}
echo "</table>";

And there you have it, a fully formed HTML table based on the rows you defined in $table.

/data/webs/external/dokuwiki/data/pages/php/using_the_gen_table_data_framework.txt · Last modified: 2021/02/10 14:48 by genadmin