dbTables Documentation


The dbTables makes creating tables simple.

<?php
// example using dbTables

$_site = require_once(getenv("SITELOADNAME"));

$S = new $_site->className($_site);
$T = new dbTables($S);

// Pass some info to getPageTopBottom method
$h->title = "Example"; // Goes in the <title></title>
$h->banner = "<h1>Example</h1>"; // becomes the <header> section
// Add some local css to but a border and padding on the table 
$h->css = <<<EOF
main table * {
  padding: .5em;
  border: 1px solid black;
}
EOF;

[$top, $footer] = $S->getPageTopBottom($h);

// create a table from the memberTable
$sql = "select * from $S->memberTable";
list($tbl) = $T->maketable($sql);

echo <<<EOF
$top
<main>
<h3>Create a table from the members database table</h3>
<p>The members table follows:</p>
$tbl
</main>
<hr>
$footer
EOF;

The 'maketable' method takes several optional arguments to help setup the table. Using the options you can give your table an id or class or set any other attributes. You can also pass a 'callback' function which can modify the rows as they are selected (see the 'example-insert-update.php' file in the 'examples' directory for more information).

  /**
   * maketable()
   * Make a full table
   *
   * @param string $query : the table query
   * @param array $extra : optional. 
   *   $extra is an optional assoc array: $extra['callback'], $extra['callback2'], $extra['footer'] and $extra['attr'].
   *   $extra['attr'] is an assoc array that can have attributes for the <table> tag, like 'id', 'title', 'class', 'style' etc.
   *   $extra['callback'] function that can modify the header after it is filled in.
   *   $extra[callback2] callback after $desc has the fields replaced with $row values.
   *   $extra['footer'] a footer string 
   * @return array [{string table}, {result}, {num}, {hdr}, table=>{string}, result=>{result}, num=>{num rows}, header=>{hdr}]
   * or === false
   */

  public function maketable($query, array $extra=null) {...}

The '$extra' argument is an associative array with the following items:

Here is an example with all of the items:

$info = $T->maketable($sql, array('callback'=>callback1, 'callback2'=>callback2,  
        'footer'=>$footer, 'attr'=>array('border'=>'1', 'class'=>'something')));
// $info[0] or $info['table'] is the table html.

There is a second dbTables method which is not used directly as much. This method is called by 'maketable'. It creates only the result rows.

  /**
   * makeresultrows
   * The $rowdesc can have a wild card like this: '<tr><td>*</td></tr>'. Then make the $extra[delim] be
   *   array("<td>", "</td>");
   * Can also have a header like '<table><thead>%<th>*</th>%</thead>'. The header delimiter is always %.
   * In both cases the fields from the query will replace the '*'.
   * Make the query fields what you want in the header using the 'as' keywork.
   * @param string $query
   * @param string $rowdesc
   * @param array $extra : $extra[delim] is an array|string with the delimiter,
   *                       $extra[return] if true the return value is an ARRAY else just a string with the rows
   *                       $extra[callback] is a callback function: calback(&$row, &$desc);
   *                       $extra[callback2] callback after $desc has the fields replaced with $row values.
   *                       $extra[header] a header template. Delimiter is % around for example '%<th>*</th>%'
   * @return string|array
   *         if $extra[return] === true then returned is an
   *            array({the row string}, {result}, {num}, {header},
   *                   rows=>{row string}, result=>{result}, num=>{number of rows}, header=>{header})
   *         else a string with the rows
   */
  
  public function makeresultrows($query, $rowdesc, array $extra=array()) {...}

dbTables Methods