~ubuntu-branches/ubuntu/gutsy/smarty/gutsy-security

« back to all changes in this revision

Viewing changes to libs/plugins/function.html_table.php

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Randrianiriana
  • Date: 2007-03-13 21:20:41 UTC
  • mfrom: (0.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313212041-dxa1s2e3pfhghc7u
Tags: 2.6.18-1
* New upstream release
* Added debian/docs
* debian/control:
  + added Homepage
  + removed Depends on debconf (Closes: #375234)
  + bumped Standards-Version to 3.7.2
  + added myself to Uploaders
* debian/copyright:
  + changed license to LGPL
  + added authors

Show diffs side-by-side

added added

removed removed

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