function.html_table.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty {html_table} function plugin
  9. *
  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. * Input:<br>
  15. * - loop = array to loop through
  16. * - cols = number of columns
  17. * - rows = number of rows
  18. * - table_attr = table attributes
  19. * - tr_attr = table row attributes (arrays are cycled)
  20. * - td_attr = table cell attributes (arrays are cycled)
  21. * - trailpad = value to pad trailing cells with
  22. * - vdir = vertical direction (default: "down", means top-to-bottom)
  23. * - hdir = horizontal direction (default: "right", means left-to-right)
  24. * - inner = inner loop (default "cols": print $loop line by line,
  25. * $loop will be printed column by column otherwise)
  26. *
  27. *
  28. * Examples:
  29. * <pre>
  30. * {table loop=$data}
  31. * {table loop=$data cols=4 tr_attr='"bgcolor=red"'}
  32. * {table loop=$data cols=4 tr_attr=$colors}
  33. * </pre>
  34. * @author Monte Ohrt <monte@ispi.net>
  35. * @version 1.0
  36. * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table}
  37. * (Smarty online manual)
  38. * @param array
  39. * @param Smarty
  40. * @return string
  41. */
  42. function smarty_function_html_table($params, &$smarty)
  43. {
  44. $table_attr = 'border="1"';
  45. $tr_attr = '';
  46. $td_attr = '';
  47. $cols = 3;
  48. $rows = 3;
  49. $trailpad = '&nbsp;';
  50. $vdir = 'down';
  51. $hdir = 'right';
  52. $inner = 'cols';
  53. if (!isset($params['loop'])) {
  54. $smarty->trigger_error("html_table: missing 'loop' parameter");
  55. return;
  56. }
  57. foreach ($params as $_key=>$_value) {
  58. switch ($_key) {
  59. case 'loop':
  60. $$_key = (array)$_value;
  61. break;
  62. case 'cols':
  63. case 'rows':
  64. $$_key = (int)$_value;
  65. break;
  66. case 'table_attr':
  67. case 'trailpad':
  68. case 'hdir':
  69. case 'vdir':
  70. case 'inner':
  71. $$_key = (string)$_value;
  72. break;
  73. case 'tr_attr':
  74. case 'td_attr':
  75. $$_key = $_value;
  76. break;
  77. }
  78. }
  79. $loop_count = count($loop);
  80. if (empty($params['rows'])) {
  81. /* no rows specified */
  82. $rows = ceil($loop_count/$cols);
  83. } elseif (empty($params['cols'])) {
  84. if (!empty($params['rows'])) {
  85. /* no cols specified, but rows */
  86. $cols = ceil($loop_count/$rows);
  87. }
  88. }
  89. $output = "<table $table_attr>\n";
  90. for ($r=0; $r<$rows; $r++) {
  91. $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n";
  92. $rx = ($vdir == 'down') ? $r*$cols : ($rows-1-$r)*$cols;
  93. for ($c=0; $c<$cols; $c++) {
  94. $x = ($hdir == 'right') ? $rx+$c : $rx+$cols-1-$c;
  95. if ($inner!='cols') {
  96. /* shuffle x to loop over rows*/
  97. $x = floor($x/$cols) + ($x%$cols)*$rows;
  98. }
  99. if ($x<$loop_count) {
  100. $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n";
  101. } else {
  102. $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n";
  103. }
  104. }
  105. $output .= "</tr>\n";
  106. }
  107. $output .= "</table>\n";
  108. return $output;
  109. }
  110. function smarty_function_html_table_cycle($name, $var, $no) {
  111. if(!is_array($var)) {
  112. $ret = $var;
  113. } else {
  114. $ret = $var[$no % count($var)];
  115. }
  116. return ($ret) ? ' '.$ret : '';
  117. }
  118. /* vim: set expandtab: */
  119. ?>