6 * @subpackage PluginsFunction
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>
21 * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
22 * {table loop=$data cols="first,second,third" tr_attr=$colors}
25 * @author Monte Ohrt <monte at ohrt dot com>
26 * @author credit to Messju Mohr <messju at lammfellpuschen dot de>
27 * @author credit to boots <boots dot smarty at yahoo dot com>
29 * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
30 * (Smarty online manual)
31 * @param array $params parameters
33 * - loop = array to loop through
34 * - cols = number of columns, comma separated list of column names
35 * or array of column names
36 * - rows = number of rows
37 * - table_attr = table attributes
38 * - th_attr = table heading attributes (arrays are cycled)
39 * - tr_attr = table row attributes (arrays are cycled)
40 * - td_attr = table cell attributes (arrays are cycled)
41 * - trailpad = value to pad trailing cells with
42 * - caption = text for caption element
43 * - vdir = vertical direction (default: "down", means top-to-bottom)
44 * - hdir = horizontal direction (default: "right", means left-to-right)
45 * - inner = inner loop (default "cols": print $loop line by line,
46 * $loop will be printed column by column otherwise)
47 * @param object $template template object
50 function smarty_function_html_table($params, $template)
52 $table_attr = 'border="1"';
56 $cols = $cols_count = 3;
65 if (!isset($params['loop'])) {
66 trigger_error("html_table: missing 'loop' parameter",E_USER_WARNING);
70 foreach ($params as $_key => $_value) {
73 $$_key = (array)$_value;
77 if (is_array($_value) && !empty($_value)) {
79 $cols_count = count($_value);
80 } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) {
81 $cols = explode(',', $_value);
82 $cols_count = count($cols);
83 } elseif (!empty($_value)) {
84 $cols_count = (int)$_value;
91 $$_key = (int)$_value;
100 $$_key = (string)$_value;
111 $loop_count = count($loop);
112 if (empty($params['rows'])) {
113 /* no rows specified */
114 $rows = ceil($loop_count / $cols_count);
115 } elseif (empty($params['cols'])) {
116 if (!empty($params['rows'])) {
117 /* no cols specified, but rows */
118 $cols_count = ceil($loop_count / $rows);
122 $output = "<table $table_attr>\n";
124 if (!empty($caption)) {
125 $output .= '<caption>' . $caption . "</caption>\n";
128 if (is_array($cols)) {
129 $cols = ($hdir == 'right') ? $cols : array_reverse($cols);
130 $output .= "<thead><tr>\n";
132 for ($r = 0; $r < $cols_count; $r++) {
133 $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>';
134 $output .= $cols[$r];
135 $output .= "</th>\n";
137 $output .= "</tr></thead>\n";
140 $output .= "<tbody>\n";
141 for ($r = 0; $r < $rows; $r++) {
142 $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
143 $rx = ($vdir == 'down') ? $r * $cols_count : ($rows-1 - $r) * $cols_count;
145 for ($c = 0; $c < $cols_count; $c++) {
146 $x = ($hdir == 'right') ? $rx + $c : $rx + $cols_count-1 - $c;
147 if ($inner != 'cols') {
148 /* shuffle x to loop over rows*/
149 $x = floor($x / $cols_count) + ($x % $cols_count) * $rows;
152 if ($x < $loop_count) {
153 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
155 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
158 $output .= "</tr>\n";
160 $output .= "</tbody>\n";
161 $output .= "</table>\n";
166 function smarty_function_html_table_cycle($name, $var, $no)
168 if (!is_array($var)) {
171 $ret = $var[$no % count($var)];
174 return ($ret) ? ' ' . $ret : '';