1 <?php
2 /**
3 * Smarty plugin
4 *
5 * @package Smarty
6 * @subpackage PluginsFunction
7 */
9 /**
10 * Smarty {html_table} function plugin
11 *
12 * Type: function<br>
13 * Name: html_table<br>
14 * Date: Feb 17, 2003<br>
15 * Purpose: make an html table from an array of data<br>
16 *
17 *
18 * Examples:
19 * <pre>
20 * {table loop=$data}
21 * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
22 * {table loop=$data cols="first,second,third" tr_attr=$colors}
23 * </pre>
24 *
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>
28 * @version 1.1
29 * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
30 * (Smarty online manual)
31 * @param array $params parameters
32 * Input:<br>
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
48 * @return string
49 */
50 function smarty_function_html_table($params, $template)
51 {
52 $table_attr = 'border="1"';
53 $tr_attr = '';
54 $th_attr = '';
55 $td_attr = '';
56 $cols = $cols_count = 3;
57 $rows = 3;
58 $trailpad = ' ';
59 $vdir = 'down';
60 $hdir = 'right';
61 $inner = 'cols';
62 $caption = '';
63 $loop = null;
65 if (!isset($params['loop'])) {
66 trigger_error("html_table: missing 'loop' parameter",E_USER_WARNING);
67 return;
68 }
70 foreach ($params as $_key => $_value) {
71 switch ($_key) {
72 case 'loop':
73 $$_key = (array)$_value;
74 break;
76 case 'cols':
77 if (is_array($_value) && !empty($_value)) {
78 $cols = $_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;
85 } else {
86 $cols_count = $cols;
87 }
88 break;
90 case 'rows':
91 $$_key = (int)$_value;
92 break;
94 case 'table_attr':
95 case 'trailpad':
96 case 'hdir':
97 case 'vdir':
98 case 'inner':
99 case 'caption':
100 $$_key = (string)$_value;
101 break;
103 case 'tr_attr':
104 case 'td_attr':
105 case 'th_attr':
106 $$_key = $_value;
107 break;
108 }
109 }
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);
119 }
120 }
122 $output = "<table $table_attr>\n";
124 if (!empty($caption)) {
125 $output .= '<caption>' . $caption . "</caption>\n";
126 }
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";
136 }
137 $output .= "</tr></thead>\n";
138 }
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;
150 }
152 if ($x < $loop_count) {
153 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
154 } else {
155 $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
156 }
157 }
158 $output .= "</tr>\n";
159 }
160 $output .= "</tbody>\n";
161 $output .= "</table>\n";
163 return $output;
164 }
166 function smarty_function_html_table_cycle($name, $var, $no)
167 {
168 if (!is_array($var)) {
169 $ret = $var;
170 } else {
171 $ret = $var[$no % count($var)];
172 }
174 return ($ret) ? ' ' . $ret : '';
175 }
177 ?>