1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| /** Static function to display an HTML table
* @param [Array] HTML Table we want to display
* @param [Integer] The number of colmns
* @param [String] The caption (if there is, else put false)
* @param [Array] What to display in the HTML table
* @param [Boolean] If there is a border
* @param [Boolean] If cell width should be calculated proportionaly as screen.with (js needed)*/
public static function table($array,$columns,$uncaption=true,$display,$border=false,$adjust=false)
{
$i = 0 ; // a pointer
$open=true; // do we have to open a <tr>
$close=false; // do we have to close the <tr>
if($border)
echo '<table border="1">';
else
echo '<table>';
if($uncaption)
{
echo '<caption>'.$uncaption.'</caption>';
}
echo '<tbody>';
foreach($array as $key => $value)
{
//THINK
$rest=$i%$columns;
if($i == 0) // first element to display
{
$open=true;
$close=false;
}
else if($i == count($array)) // last element to display
{
$close=true;
$open=false;
}
else if($rest == 0) // open an other <tr>
{
$close=true;
$open=true;
}
else
{
$open=false;
$close=false;
}
$i++;
// WRITE
// -- COLUMN
if($open && !$close)
{
echo '<tr>';
}
else if($open && $close)
{
echo '</tr>';
echo '<tr>';
}
else if(!$open && $close)
{
echo '</tr>';
}
// -- CELL
if((isset($_SESSION['width']) && $_SESSION['width'] > 0) && $adjust) // to arrange td width if javascript is enable
{
echo '<td style="width: '.(70/100*$_SESSION['width']/$columns).'px;">';
}
else
{
echo '<td>';
}
for($k = count($display) - 1 ; $k >=0; $k--)
{
foreach($display[$k] as $p => $v)
{
if($p=='markup')
{
if(is_array($v))
{
foreach($v as $z=>$e)
{
echo '<'.$z; // START MARKUP
if(is_array($e))
{
for($l = count($e) -1 ; $l >= 0 ; $l--)
{
echo ' '.substr($e[$l],0,strpos($e[$l],"=")+1);
// ATTRIBUTE VALUE
echo '"'.substr($e[$l],strpos($e[$l],"=")+1).$key.'"';
}
}
echo '>'; // END MARKUP
echo $value;
echo '</'.$z.'>'; // CLOSE MARKUP
}
}
}
}
}
echo '</td>';
}
echo '</tbody>';
echo '</table>';
} |
Partager