You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.html_table.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_table} function plugin
  10. * Type: function<br>
  11. * Name: html_table<br>
  12. * Date: Feb 17, 2003<br>
  13. * Purpose: make an html table from an array of data<br>
  14. * Params:
  15. * <pre>
  16. * - loop - array to loop through
  17. * - cols - number of columns, comma separated list of column names
  18. * or array of column names
  19. * - rows - number of rows
  20. * - table_attr - table attributes
  21. * - th_attr - table heading attributes (arrays are cycled)
  22. * - tr_attr - table row attributes (arrays are cycled)
  23. * - td_attr - table cell attributes (arrays are cycled)
  24. * - trailpad - value to pad trailing cells with
  25. * - caption - text for caption element
  26. * - vdir - vertical direction (default: "down", means top-to-bottom)
  27. * - hdir - horizontal direction (default: "right", means left-to-right)
  28. * - inner - inner loop (default "cols": print $loop line by line,
  29. * $loop will be printed column by column otherwise)
  30. * </pre>
  31. * Examples:
  32. * <pre>
  33. * {table loop=$data}
  34. * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
  35. * {table loop=$data cols="first,second,third" tr_attr=$colors}
  36. * </pre>
  37. *
  38. * @author Monte Ohrt <monte at ohrt dot com>
  39. * @author credit to Messju Mohr <messju at lammfellpuschen dot de>
  40. * @author credit to boots <boots dot smarty at yahoo dot com>
  41. * @version 1.1
  42. * @link http://www.smarty.net/manual/en/language.function.html.table.php {html_table}
  43. * (Smarty online manual)
  44. *
  45. * @param array $params parameters
  46. *
  47. * @return string
  48. */
  49. function smarty_function_html_table($params)
  50. {
  51. $table_attr = 'border="1"';
  52. $tr_attr = '';
  53. $th_attr = '';
  54. $td_attr = '';
  55. $cols = $cols_count = 3;
  56. $rows = 3;
  57. $trailpad = '&nbsp;';
  58. $vdir = 'down';
  59. $hdir = 'right';
  60. $inner = 'cols';
  61. $caption = '';
  62. $loop = null;
  63. if (!isset($params['loop'])) {
  64. trigger_error("html_table: missing 'loop' parameter", E_USER_WARNING);
  65. return;
  66. }
  67. foreach ($params as $_key => $_value) {
  68. switch ($_key) {
  69. case 'loop':
  70. $$_key = (array) $_value;
  71. break;
  72. case 'cols':
  73. if (is_array($_value) && !empty($_value)) {
  74. $cols = $_value;
  75. $cols_count = count($_value);
  76. } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) {
  77. $cols = explode(',', $_value);
  78. $cols_count = count($cols);
  79. } elseif (!empty($_value)) {
  80. $cols_count = (int) $_value;
  81. } else {
  82. $cols_count = $cols;
  83. }
  84. break;
  85. case 'rows':
  86. $$_key = (int) $_value;
  87. break;
  88. case 'table_attr':
  89. case 'trailpad':
  90. case 'hdir':
  91. case 'vdir':
  92. case 'inner':
  93. case 'caption':
  94. $$_key = (string) $_value;
  95. break;
  96. case 'tr_attr':
  97. case 'td_attr':
  98. case 'th_attr':
  99. $$_key = $_value;
  100. break;
  101. }
  102. }
  103. $loop_count = count($loop);
  104. if (empty($params['rows'])) {
  105. /* no rows specified */
  106. $rows = ceil($loop_count / $cols_count);
  107. } elseif (empty($params['cols'])) {
  108. if (!empty($params['rows'])) {
  109. /* no cols specified, but rows */
  110. $cols_count = ceil($loop_count / $rows);
  111. }
  112. }
  113. $output = "<table $table_attr>\n";
  114. if (!empty($caption)) {
  115. $output .= '<caption>' . $caption . "</caption>\n";
  116. }
  117. if (is_array($cols)) {
  118. $cols = ($hdir == 'right') ? $cols : array_reverse($cols);
  119. $output .= "<thead><tr>\n";
  120. for ($r = 0; $r < $cols_count; $r ++) {
  121. $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>';
  122. $output .= $cols[$r];
  123. $output .= "</th>\n";
  124. }
  125. $output .= "</tr></thead>\n";
  126. }
  127. $output .= "<tbody>\n";
  128. for ($r = 0; $r < $rows; $r ++) {
  129. $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
  130. $rx = ($vdir == 'down') ? $r * $cols_count : ($rows - 1 - $r) * $cols_count;
  131. for ($c = 0; $c < $cols_count; $c ++) {
  132. $x = ($hdir == 'right') ? $rx + $c : $rx + $cols_count - 1 - $c;
  133. if ($inner != 'cols') {
  134. /* shuffle x to loop over rows*/
  135. $x = floor($x / $cols_count) + ($x % $cols_count) * $rows;
  136. }
  137. if ($x < $loop_count) {
  138. $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
  139. } else {
  140. $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
  141. }
  142. }
  143. $output .= "</tr>\n";
  144. }
  145. $output .= "</tbody>\n";
  146. $output .= "</table>\n";
  147. return $output;
  148. }
  149. function smarty_function_html_table_cycle($name, $var, $no)
  150. {
  151. if (!is_array($var)) {
  152. $ret = $var;
  153. } else {
  154. $ret = $var[$no % count($var)];
  155. }
  156. return ($ret) ? ' ' . $ret : '';
  157. }