From array to HTML table - very simple

This is a very simple function to create a HTML table from an array. Some things are even hardcoded but can easily be either changed or modified to be more generic.

 

 

<?php
function zim_table($headers, $rows, $title = FALSE, $class = 'sortable zimonitor'){
   
    if (
$title) {
       
$title = '<h2>'.$title.'</h2>';
    }
   
   
$table = '<table border="0" width="100%" cellpadding="0" style="border-collapse: collapse" bordercolor="#000000" class="'.$class.'">';

    if(
is_array($headers)){
       
$table .= '<thead><tr class="listtext_menu">';
        foreach (
$headers as $key => $th) {
           
$table .= '<th width="" bgcolor="#C0C0C0">'.$th.'</th>';
        }
       
$table .= '</thead></tr>';
    }
   
    if(
is_array($rows)){
       
$table .= '<tbody>';
        foreach (
$rows as $rk => $row) {
           
$table .= '<tr class="listtext_menu">';
           
$bgcolor  = $q++ % 2 ? '#EAEAEA' : '#ffffff';
                foreach (
$row as $tk => $td) {
                   
$table .= '<td width="" bgcolor="'.$bgcolor.'">'.$td.'</td>';
                }
           
$table .= '</tr>';
        }
       
$table .= '</tbody>';
    }
   
   
$table .= '</table>';
   
    return (
$title ? $title : "") . $table;
}
?>

This is an example of implementation

<?php
    $sql
= mysql_query("SELECT `zimcloud_installations`.`id`, `zimcloud_installations`.`name`,
                        `zimcloud_installations`.`status` , `zimcloud_installations`.`chargify_state_id`, `chargify_states`.*, `zimcloud_status`.*
                        FROM  `zimcloud_installations`
                        LEFT JOIN  `chargify_states` ON (  `chargify_states`.id = `zimcloud_installations`.`chargify_state_id`)
                        LEFT JOIN  `zimcloud_status` ON (  `zimcloud_status`.id = `zimcloud_installations`.`status`)
                        WHERE  `zimcloud_installations`.`chargify_state_id` = 4 AND `zimcloud_installations`.`status` != 7"
);

    if(
$sql){
       
$headers = array('id', 'installation', 'Babylon status', 'Chargify status');
        while (
$row = mysql_fetch_array($sql)) {
           
$rows[] = array($row['id'], $row['name'], $row['fulltext'], $row['label']);   
        }
       
        if(
count($rows) > 0){
            echo
zim_table($headers, $rows , zim_message('Mismatch in status - change status to Active in Babylon', 'warning'));
        }else{
            echo
zim_message("All active instances in Chargify is active in Babylon", "ok");
        }
       
       
    }else{
        echo
zim_message("SQL error: Couldn't check if all active instances in Chargify is active in Babylon", "error");
    }
?>

This is also a little simple but handy message function

<?php
function zim_message($message, $type = "message"){
   
    switch (
$type) :
        case 
"message": $style = ''; break;
        case 
"error":   $style = 'color: red;'; break;
        case 
"warning": $style = 'color: #fc7a02;'; break;
        case 
"ok":      $style = 'color: green;'; break;
        default:
$style = '';
    endswitch;
   
    return
'<div style="text-align: left; width: 100%; padding: 2px; '.$style.'">'.$message.'</div>';
}
?>
Knowledge keywords: