<?php
// html.inc.php

function html_font($text,$font="times",$font_size="12px",$source_friendly=true) {
         return (
         
$source_friendly true ?
         
"\n<font style=\"font-family: $font;font-size: $font_size\" face=$font>\n$text\n</font>\n"
         
:
         
"<font style=\"font-family: $font;font-size: $font_size\" face=$font>$text</font>"
         
);
}
function 
html_br($source_friendly=true) {
         return (
         
$source_friendly true ?
         
"\n<br />\n"
         
:
         
"<br />"
         
);
}

function 
html_table($arr,$with_headers=true,$with_lines=false,$falign='right',
                    
$table="border=1 bordercolor=black cellspacing=0 cellpadding=2 align=center valign=top") {
         
$sep_th_pre="\n<th valign='top' align='center'>\n";
         
$sep_th_post="&nbsp;</th>\n";
         
$sep_field_pre="\n<td valign='top' align='$falign'>\n";
         
$sep_field_post="&nbsp;</td>\n";
         
$sep=$sep_field_post.$sep_field_pre;
         
$sep_row_pre="\n<tr>\n";
         
$sep_row_post="\n</tr>\n";
         
$table_pre="\n<table $table>\n";
         
$table_post="</table>";
         if (
$with_lines==false) {
                if (
$with_headers==true) {
                        
$keys=array_keys(current($arr));
                        
$rows[]=$sep_th_pre.@implode($sep_th_post.$sep_th_pre,$keys).$sep_th_post;
                }
                foreach(
$arr as $key=>$row) {
/*                        foreach($row as $field) {
                                 $field=round($field,2); }
                                //if (is_float($field)) echo "is_float"; }
*/
                        
$rows[]=$sep_field_pre.implode($sep,$row).$sep_field_post;
                }
         }
         if (
$with_lines==true) {
                if (
$with_headers==true) {
                        
$keys=array_keys(current($arr));
                        
$rows[]=$sep_th_pre."&nbsp;".$sep_th_post
                                 
.$sep_th_pre.implode($sep_th_post.$sep_th_pre,$keys).$sep_th_post;
                }
                foreach(
$arr as $key=>$row) {
                        foreach(
$row as $field) {
                                 
$field=round($field,2); }
                                
//if (is_float($field)) echo "is_float"; }
                        
$rows[]="$sep_field_pre<b>$key</b>$sep_field_post"
                                
.$sep_field_pre.implode($sep_field_post.$sep_field_pre,$row).$sep_field_post;
                }
         }
         
$table=implode($sep_row_post.$sep_row_pre,$rows);
         
$table=$table_pre.$sep_row_pre.$table.$sep_row_post.$table_post;
         
nl2br($table);
         return 
$table;
}

function 
html_table_site($arr,$with_headers=true,$with_lines=false,
                    
$table="border=0 bordercolor=black cellspacing=1
                     cellpadding=2 align=left valign=top width=100% height=100%"
) {
         
$sep_th_pre="\n<th valign='top' align='center'>\n";
         
$sep_th_post="&nbsp;</th>\n";
         
$sep_field_pre="\n<td valign='top' align='left'>\n";
         
$sep_field_post="&nbsp;</td>\n";
         
$sep_row_pre="\n<tr>\n";
         
$sep_row_post="\n</tr>\n";
         
$table_pre="\n<table $table>\n";
         
$table_post="</table>";
         if (
$with_lines==false) {
                if (
$with_headers==true) {
                        
$keys=array_keys(current($arr));
                        
$rows[]=$sep_th_pre.implode($sep_th_post.$sep_th_pre,$keys).$sep_th_post;
                }
                foreach(
$arr as $key=>$row) {
                        foreach(
$row as $field) {
                                 
$field=round($field,2); }
                                
//if (is_float($field)) echo "is_float"; }
                        
$rows[]=$sep_field_pre.implode($sep_field_post.$sep_field_pre,$row).$sep_field_post;
                }
         }
         if (
$with_lines==true) {
                if (
$with_headers==true) {
                        
$keys=array_keys(current($arr));
                        
$rows[]=$sep_th_pre."&nbsp;".$sep_th_post
                                 
.$sep_th_pre.implode($sep_th_post.$sep_th_pre,$keys).$sep_th_post;
                }
                foreach(
$arr as $key=>$row) {
                        foreach(
$row as $field) {
                                 
$field=round($field,2); }
                                
//if (is_float($field)) echo "is_float"; }
                        
$rows[]="$sep_field_pre<b>$key</b>$sep_field_post"
                                
.$sep_field_pre.implode($sep_field_post.$sep_field_pre,$row).$sep_field_post;
                }
         }
         
$table=implode($sep_row_post.$sep_row_pre,$rows);
         
$table=$table_pre.$sep_row_pre.$table.$sep_row_post.$table_post;
         return 
$table;
}

// Create anchors from an array
// If the keys are defined and strings,
// the href are the keys
// If the keys are numbers, the href are the elements of the array
function html_a($arr) {
         
$ret="";
         foreach (
$arr as $key=>$elem) {
                 if (
is_string($key)) {
                    
$ret.="<a href=$key>$elem</a>\n";
                    
$ret.=html_br();
                 } else {
                 
$ret.="<a href=\"$elem\">$elem</a>\n";
                 
$ret.=html_br();
                 }
         }
         return 
$ret;
}


// $returns a print_r "bettered"
// $with_pre
function print_n($data$return=false$with_pre=true) {
         if (
is_array($data)) { $ret=print_r($data,true); }
         else { 
$ret=@htmlspecialchars((string)$data); }
         if (
$with_pre==true) { $ret="<pre>".$ret."</pre>"; }
         if (
$return==false) { print $ret; return; }
         return 
$ret;
}

?>