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