1: <?php
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: function smarty_function_html_table($params)
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;
64:
65: if (!isset($params['loop'])) {
66: trigger_error("html_table: missing 'loop' parameter", E_USER_WARNING);
67:
68: return;
69: }
70:
71: foreach ($params as $_key => $_value) {
72: switch ($_key) {
73: case 'loop':
74: $$_key = (array) $_value;
75: break;
76:
77: case 'cols':
78: if (is_array($_value) && !empty($_value)) {
79: $cols = $_value;
80: $cols_count = count($_value);
81: } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) {
82: $cols = explode(',', $_value);
83: $cols_count = count($cols);
84: } elseif (!empty($_value)) {
85: $cols_count = (int) $_value;
86: } else {
87: $cols_count = $cols;
88: }
89: break;
90:
91: case 'rows':
92: $$_key = (int) $_value;
93: break;
94:
95: case 'table_attr':
96: case 'trailpad':
97: case 'hdir':
98: case 'vdir':
99: case 'inner':
100: case 'caption':
101: $$_key = (string) $_value;
102: break;
103:
104: case 'tr_attr':
105: case 'td_attr':
106: case 'th_attr':
107: $$_key = $_value;
108: break;
109: }
110: }
111:
112: $loop_count = count($loop);
113: if (empty($params['rows'])) {
114:
115: $rows = ceil($loop_count / $cols_count);
116: } elseif (empty($params['cols'])) {
117: if (!empty($params['rows'])) {
118:
119: $cols_count = ceil($loop_count / $rows);
120: }
121: }
122:
123: $output = "<table $table_attr>\n";
124:
125: if (!empty($caption)) {
126: $output .= '<caption>' . $caption . "</caption>\n";
127: }
128:
129: if (is_array($cols)) {
130: $cols = ($hdir == 'right') ? $cols : array_reverse($cols);
131: $output .= "<thead><tr>\n";
132:
133: for ($r = 0; $r < $cols_count; $r ++) {
134: $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>';
135: $output .= $cols[$r];
136: $output .= "</th>\n";
137: }
138: $output .= "</tr></thead>\n";
139: }
140:
141: $output .= "<tbody>\n";
142: for ($r = 0; $r < $rows; $r ++) {
143: $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
144: $rx = ($vdir == 'down') ? $r * $cols_count : ($rows - 1 - $r) * $cols_count;
145:
146: for ($c = 0; $c < $cols_count; $c ++) {
147: $x = ($hdir == 'right') ? $rx + $c : $rx + $cols_count - 1 - $c;
148: if ($inner != 'cols') {
149:
150: $x = floor($x / $cols_count) + ($x % $cols_count) * $rows;
151: }
152:
153: if ($x < $loop_count) {
154: $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
155: } else {
156: $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
157: }
158: }
159: $output .= "</tr>\n";
160: }
161: $output .= "</tbody>\n";
162: $output .= "</table>\n";
163:
164: return $output;
165: }
166:
167: function smarty_function_html_table_cycle($name, $var, $no)
168: {
169: if (!is_array($var)) {
170: $ret = $var;
171: } else {
172: $ret = $var[$no % count($var)];
173: }
174:
175: return ($ret) ? ' ' . $ret : '';
176: }
177: