source: trunk/www.guidonia.net/wp/wp-content/plugins/odlinks/includes/Smarty/plugins/function.html_table.php@ 44

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