10 * Smarty {html_table} function plugin
13 * Name: html_table<br>
14 * Date: Feb 17, 2003<br>
15 * Purpose: make an html table from an array of data<br>
17 * - loop = array to loop through
18 * - cols = number of columns
19 * - rows = number of rows
20 * - table_attr = table attributes
21 * - tr_attr = table row attributes (arrays are cycled)
22 * - td_attr = table cell attributes (arrays are cycled)
23 * - trailpad = value to pad trailing cells with
24 * - vdir = vertical direction (default: "down", means top-to-bottom)
25 * - hdir = horizontal direction (default: "right", means left-to-right)
26 * - inner = inner loop (default "cols": print $loop line by line,
27 * $loop will be printed column by column otherwise)
33 * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
34 * {table loop=$data cols=4 tr_attr=$colors}
36 * @author Monte Ohrt <monte at ohrt dot com>
38 * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
39 * (Smarty online manual)
44 function smarty_function_html_table($params, &$smarty)
46 $table_attr = 'border="1"';
56 if (!isset($params['loop'])) {
57 $smarty->trigger_error("html_table: missing 'loop' parameter");
61 foreach ($params as $_key=>$_value) {
64 $
$_key = (array)$_value;
69 $
$_key = (int)$_value;
77 $
$_key = (string)$_value;
87 $loop_count = count($loop);
88 if (empty($params['rows'])) {
89 /* no rows specified */
90 $rows = ceil($loop_count/$cols);
91 } elseif (empty($params['cols'])) {
92 if (!empty($params['rows'])) {
93 /* no cols specified, but rows */
94 $cols = ceil($loop_count/$rows);
98 $output = "<table $table_attr>\n";
100 for ($r=0; $r<$rows; $r++
) {
101 $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
102 $rx = ($vdir == 'down') ?
$r*$cols : ($rows-1-$r)*$cols;
104 for ($c=0; $c<$cols; $c++
) {
105 $x = ($hdir == 'right') ?
$rx+
$c : $rx+
$cols-1-$c;
106 if ($inner!='cols') {
107 /* shuffle x to loop over rows*/
108 $x = floor($x/$cols) +
($x%
$cols)*$rows;
111 if ($x<$loop_count) {
112 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
114 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
117 $output .= "</tr>\n";
119 $output .= "</table>\n";
124 function smarty_function_html_table_cycle($name, $var, $no) {
125 if(!is_array($var)) {
128 $ret = $var[$no %
count($var)];
131 return ($ret) ?
' '.$ret : '';
135 /* vim: set expandtab: */