Table functions - from array to HTML table

Some functions useful when generating HTML tables from an array. Note that themeTabel() is standalone, but themeTable2() requires to supporting functions themeAttributes() and themeTableCell().

 

<?php
function themeTable($table_list, $header, $class = 'barcodes', $title = FALSE) {

        if (
$title) {
           
$html = '<div class="' . $class . '">' . $title . "</div>\n";
        }

       
$html .= '<table class="' . $class . '">' . "\n";

       
$header_th = ' <tr>' . "\n";
        foreach (
$header as $key => $th) {
           
$header_th .= '  <th class="' . $key . '">' . $th . '</th>' . "\n";
        }
       
$header_th .= '</tr>' . "\n";

       
$c = FALSE;
        foreach (
$table_list as $key => $package) {

           
$header_td .= ' <tr class="' . (($c = !$c) ? 'odd' : 'even') . ' ' . $package['class'] . '" title="' . $package['title'] . '">' . "\n";


            if (
is_array($package) AND ! empty($package['data'])) {

               
$header_td .= '  <td class="' . $package['class'] . '">' . $package['data'] . '</td>' . "\n";
            } elseif (!empty(
$package['rows'])) {
               
$header_td .= '  <td class="' . $key . '">';
                foreach (
$package as $key => $td) {
                   
$header_td .= ' ' . $td . "\n";
                }
               
$header_td .= '  </td>';
            } elseif (
is_array($package)) {
               
$header_td .= '  <td class="' . $key . '">';
                foreach (
$package as $key => $td) {
                   
$header_td .= ' ' . $td . "\n";
                }
               
$header_td .= '  </td>';
            } else {
               
$header_td .= '  <td class="' . $key . '">' . $package . '</td>' . "\n";
            }

           
$header_td .= '</tr>' . "\n";
        }

       
$html .= $header_th . $header_td . '</table>' . "\n";

        return
$html;
    }
?>

The following three functions are all needed to run themeTable2()

<?php
   
/**
     * Helper function to themeTable2
     * @param type $attributes
     * @return string
     */
   
private function themeAttributes($attributes = array()) {
        if (
is_array($attributes) AND ! empty($attributes)) {
           
$t = '';

            foreach (
$attributes as $key => $value) {
               
$t .= " $key=" . '"' . (!empty($value) ? htmlspecialchars($value, ENT_QUOTES, 'UTF-8') : '&nbsp;') . '"';
            }
            return
$t;
        }
    }

   
       
/**
     * Helper function to themeTable2
     * @param type $attributes
     * @return string
     */
   
private function themeTableCell($cell, $header = FALSE) {
       
$attributes = '';

        if (
is_array($cell)) {
           
$data = isset($cell['data']) ? $cell['data'] : '';
           
$header |= isset($cell['header']);
            unset(
$cell['data']);
            unset(
$cell['header']);

           
$attributes = $this->themeAttributes($cell);
        } else {
           
$data = $cell;
        }

        if (
$header) {
           
$output = "<th$attributes>$data</th>";
        } else {
           
$output = "<td$attributes>$data</td>";
        }

        return
$output;
    }
   
      
/**
     * Generic table function based on Drupal 6 theme_table
     * @param type $header
     * @param type $rows
     * @param type $attributes
     * @param type $caption
     * @param type $footer array; class, data
     * @return string
     */
   
public function themeTable2($header, $rows, $attributes = array(), $caption = NULL, $footer = NULL) {

       
$output = '<table' . $this->themeAttributes($attributes) . ">\n";

        if (isset(
$caption)) {
           
$output .= '<caption>' . $caption . "</caption>\n";
        }

       
// Format the table header:
       
if (count($header) AND count($rows)) {

           
$output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
            foreach (
$header as $cell) {
               
$output .= $this->themeTableCell($cell, TRUE);
            }
           
// Using ternary operator to close the tags based on whether or not there are rows
           
$output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
        } else {
           
$ts = array();
        }

       
// Format the table rows:
       
if (count($rows)) {
           
$output .= "<tbody>\n";
           
$flip = array('even' => 'odd', 'odd' => 'even');
           
$class = 'even';
            foreach (
$rows as $number => $row) {
               
$attributes = array();

               
// Check if we're dealing with a simple or complex row
               
if (isset($row['data'])) {
                    foreach (
$row as $key => $value) {
                        if (
$key == 'data') {
                           
$cells = $value;
                        } else {
                           
$attributes[$key] = $value;
                        }
                    }
                } else {
                   
$cells = $row;
                }
                if (
count($cells)) {
                   
// Add odd/even class
                   
$class = $flip[$class];
                    if (isset(
$attributes['class'])) {
                       
$attributes['class'] .= ' ' . $class;
                    } else {
                       
$attributes['class'] = $class;
                    }

                   
// Build row
                   
$output .= ' <tr' . $this->themeAttributes($attributes) . '>';
                   
$i = 0;
                    foreach (
$cells as $cell) {
                       
//$cell = tablesort_cell($cell, $header, $ts, $i++);drupal
                       
$output .= $this->themeTableCell($cell);
                    }
                   
$output .= " </tr>\n";
                }
            }
           
$output .= "</tbody>\n";
        }

       
$output .= "</table>\n";

        if (isset(
$footer['data'])) {
           
$output .= '<div class="table-footer '.$footer['class'].'">' . $footer['data'] . "</div>\n";
        }
        return
$output;
    }
                       
?>
Knowledge keywords: