blob: bfe45799380bed396b41ef5e4f49d98f02a291c5 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001################
2HTML Table Class
3################
4
5The Table Class provides functions that enable you to auto-generate HTML
6tables from arrays or database result sets.
7
Andrey Andreevcc042092014-01-03 17:08:27 +02008.. contents::
9 :local:
Andrey Andreev0ee2a632013-09-23 18:55:38 +030010
11.. raw:: html
12
Andrey Andreevcc042092014-01-03 17:08:27 +020013 <div class="custom-index container"></div>
Andrey Andreev0ee2a632013-09-23 18:55:38 +030014
15*********************
16Using the Table Class
17*********************
18
Derek Jones8ede1a22011-10-05 13:34:52 -050019Initializing the Class
20======================
21
22Like most other classes in CodeIgniter, the Table class is initialized
Andrey Andreev0ee2a632013-09-23 18:55:38 +030023in your controller using the ``$this->load->library()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -050024
25 $this->load->library('table');
26
Andrey Andreev0ee2a632013-09-23 18:55:38 +030027Once loaded, the Table library object will be available using::
28
29 $this->table
Derek Jones8ede1a22011-10-05 13:34:52 -050030
31Examples
32========
33
34Here is an example showing how you can create a table from a
35multi-dimensional array. Note that the first array index will become the
Andrey Andreev0ee2a632013-09-23 18:55:38 +030036table heading (or you can set your own headings using the ``set_heading()``
37method described in the function reference below).
Derek Jones8ede1a22011-10-05 13:34:52 -050038
39::
40
Derek Jonesd4678622011-10-05 15:40:24 -050041 $this->load->library('table');
42
43 $data = array(
Andrey Andreev0ee2a632013-09-23 18:55:38 +030044 array('Name', 'Color', 'Size'),
45 array('Fred', 'Blue', 'Small'),
46 array('Mary', 'Red', 'Large'),
47 array('John', 'Green', 'Medium')
48 );
Derek Jonesd4678622011-10-05 15:40:24 -050049
50 echo $this->table->generate($data);
Derek Jones8ede1a22011-10-05 13:34:52 -050051
52Here is an example of a table created from a database query result. The
53table class will automatically generate the headings based on the table
Andrey Andreev0ee2a632013-09-23 18:55:38 +030054names (or you can set your own headings using the ``set_heading()``
55method described in the class reference below).
Derek Jones8ede1a22011-10-05 13:34:52 -050056
57::
58
Derek Jonesd4678622011-10-05 15:40:24 -050059 $this->load->library('table');
60
Andrey Andreev0ee2a632013-09-23 18:55:38 +030061 $query = $this->db->query('SELECT * FROM my_table');
Derek Jonesd4678622011-10-05 15:40:24 -050062
63 echo $this->table->generate($query);
Derek Jones8ede1a22011-10-05 13:34:52 -050064
65Here is an example showing how you might create a table using discrete
66parameters::
67
Derek Jonesd4678622011-10-05 15:40:24 -050068 $this->load->library('table');
69
70 $this->table->set_heading('Name', 'Color', 'Size');
71
72 $this->table->add_row('Fred', 'Blue', 'Small');
73 $this->table->add_row('Mary', 'Red', 'Large');
74 $this->table->add_row('John', 'Green', 'Medium');
75
76 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -050077
78Here is the same example, except instead of individual parameters,
79arrays are used::
80
Derek Jonesd4678622011-10-05 15:40:24 -050081 $this->load->library('table');
82
83 $this->table->set_heading(array('Name', 'Color', 'Size'));
84
85 $this->table->add_row(array('Fred', 'Blue', 'Small'));
86 $this->table->add_row(array('Mary', 'Red', 'Large'));
87 $this->table->add_row(array('John', 'Green', 'Medium'));
88
89 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -050090
91Changing the Look of Your Table
92===============================
93
94The Table Class permits you to set a table template with which you can
95specify the design of your layout. Here is the template prototype::
96
Andrey Andreev0ee2a632013-09-23 18:55:38 +030097 $template = array(
98 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
Derek Jonesd4678622011-10-05 15:40:24 -050099
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300100 'heading_row_start' => '<tr>',
101 'heading_row_end' => '</tr>',
102 'heading_cell_start' => '<th>',
103 'heading_cell_end' => '</th>',
Derek Jonesd4678622011-10-05 15:40:24 -0500104
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300105 'row_start' => '<tr>',
106 'row_end' => '</tr>',
107 'cell_start' => '<td>',
108 'cell_end' => '</td>',
Derek Jonesd4678622011-10-05 15:40:24 -0500109
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300110 'row_alt_start' => '<tr>',
111 'row_alt_end' => '</tr>',
112 'cell_alt_start' => '<td>',
113 'cell_alt_end' => '</td>',
Derek Jonesd4678622011-10-05 15:40:24 -0500114
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300115 'table_close' => '</table>'
116 );
Derek Jonesd4678622011-10-05 15:40:24 -0500117
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300118 $this->table->set_template($template);
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
120.. note:: You'll notice there are two sets of "row" blocks in the
121 template. These permit you to create alternating row colors or design
122 elements that alternate with each iteration of the row data.
123
124You are NOT required to submit a complete template. If you only need to
125change parts of the layout you can simply submit those elements. In this
126example, only the table opening tag is being changed::
127
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300128 $template = array(
129 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
130 );
Derek Jonesd4678622011-10-05 15:40:24 -0500131
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300132 $this->table->set_template($template);
Mike Funk46e3a9a2012-02-24 09:38:35 -0500133
134You can also set defaults for these in a config file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500135
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300136***************
137Class Reference
138***************
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300140.. class:: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300142 .. attribute:: $function = FALSE
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300144 Allows you to specify a native PHP function or a valid function array object to be applied to all cell data.
145 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300147 $this->load->library('table');
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300149 $this->table->set_heading('Name', 'Color', 'Size');
150 $this->table->add_row('Fred', '<strong>Blue</strong>', 'Small');
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300152 $this->table->function = 'htmlspecialchars';
153 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300155 In the above example, all cell data would be ran through PHP's :php:func:`htmlspecialchars()` function, resulting in::
Derek Jones8ede1a22011-10-05 13:34:52 -0500156
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300157 <td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>
Derek Jones8ede1a22011-10-05 13:34:52 -0500158
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300159 .. method:: generate([$table_data = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300161 :param mixed $table_data: data to populate the table rows with
162 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500163
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300164 Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300166 .. method:: set_caption($caption)
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300168 :param string $caption: table caption
169 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300171 Permits you to add a caption to the table.
172 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300174 $this->table->set_caption('Colors');
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300176 .. method:: set_heading([$args = array()[, ...]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300178 :param mixed $args: an array or multiple strings containing the table column titles
179 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300181 Permits you to set the table heading. You can submit an array or discrete params::
Derek Jonesd4678622011-10-05 15:40:24 -0500182
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300183 $this->table->set_heading('Name', 'Color', 'Size');
Derek Jones8ede1a22011-10-05 13:34:52 -0500184
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300185 $this->table->set_heading(array('Name', 'Color', 'Size'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500186
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300187 .. method:: add_row([$args = array()[, ...]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300189 :param mixed $args: an array or multiple strings containing the row values
190 :returns: void
Derek Jonesd4678622011-10-05 15:40:24 -0500191
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300192 Permits you to add a row to your table. You can submit an array or discrete params::
Derek Jonesd4678622011-10-05 15:40:24 -0500193
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300194 $this->table->add_row('Blue', 'Red', 'Green');
Derek Jonesd4678622011-10-05 15:40:24 -0500195
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300196 $this->table->add_row(array('Blue', 'Red', 'Green'));
Derek Jonesd4678622011-10-05 15:40:24 -0500197
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300198 If you would like to set an individual cell's tag attributes, you can use an associative array for that cell.
199 The associative key **data** defines the cell's data. Any other key => val pairs are added as key='val' attributes to the tag::
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300201 $cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);
202 $this->table->add_row($cell, 'Red', 'Green');
Derek Jones8ede1a22011-10-05 13:34:52 -0500203
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300204 // generates
205 // <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300207 .. method:: make_columns([$array = array()[, $col_limit = 0]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300209 :param array $array: an array containing multiple rows' data
210 :param int $col_limit: count of columns in the table
211 :returns: array
Derek Jonesd4678622011-10-05 15:40:24 -0500212
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300213 This method takes a one-dimensional array as input and creates a multi-dimensional array with a depth equal to the number of columns desired.
214 This allows a single array with many elements to be displayed in a table that has a fixed column count. Consider this example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500215
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300216 $list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
Derek Jones8ede1a22011-10-05 13:34:52 -0500217
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300218 $new_list = $this->table->make_columns($list, 3);
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300220 $this->table->generate($new_list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500221
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300222 // Generates a table with this prototype
Derek Jones8ede1a22011-10-05 13:34:52 -0500223
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300224 <table border="0" cellpadding="4" cellspacing="0">
225 <tr>
226 <td>one</td><td>two</td><td>three</td>
227 </tr><tr>
228 <td>four</td><td>five</td><td>six</td>
229 </tr><tr>
230 <td>seven</td><td>eight</td><td>nine</td>
231 </tr><tr>
232 <td>ten</td><td>eleven</td><td>twelve</td></tr>
233 </table>
Derek Jones8ede1a22011-10-05 13:34:52 -0500234
Derek Jonesd4678622011-10-05 15:40:24 -0500235
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300236 .. method:: set_template($template)
Derek Jonesd4678622011-10-05 15:40:24 -0500237
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300238 :param array $template: associative array containing template values
239 :returns: bool
Derek Jonesd4678622011-10-05 15:40:24 -0500240
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300241 Permits you to set your template. You can submit a full or partial template.
242 ::
Derek Jonesd4678622011-10-05 15:40:24 -0500243
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300244 $template = array(
245 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
246 );
Derek Jonesd4678622011-10-05 15:40:24 -0500247
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300248 $this->table->set_template($template);
Derek Jones8ede1a22011-10-05 13:34:52 -0500249
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300250 .. method:: set_empty($value)
Derek Jones8ede1a22011-10-05 13:34:52 -0500251
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300252 :param mixed $value: value to put in empty cells
253 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300255 Lets you set a default value for use in any table cells that are empty.
256 You might, for example, set a non-breaking space::
Derek Jones8ede1a22011-10-05 13:34:52 -0500257
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300258 $this->table->set_empty("&nbsp;");
Derek Jonesd4678622011-10-05 15:40:24 -0500259
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300260 .. method:: clear()
Derek Jonesd4678622011-10-05 15:40:24 -0500261
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300262 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -0500263
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300264 Lets you clear the table heading and row data. If you need to show multiple tables with different data you should to call this method
265 after each table has been generated to clear the previous table information. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500266
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300267 $this->load->library('table');
Derek Jones8ede1a22011-10-05 13:34:52 -0500268
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300269 $this->table->set_heading('Name', 'Color', 'Size');
270 $this->table->add_row('Fred', 'Blue', 'Small');
271 $this->table->add_row('Mary', 'Red', 'Large');
272 $this->table->add_row('John', 'Green', 'Medium');
273
274 echo $this->table->generate();
275
276 $this->table->clear();
277
278 $this->table->set_heading('Name', 'Day', 'Delivery');
279 $this->table->add_row('Fred', 'Wednesday', 'Express');
280 $this->table->add_row('Mary', 'Monday', 'Air');
281 $this->table->add_row('John', 'Saturday', 'Overnight');
282
Andrey Andreevcc042092014-01-03 17:08:27 +0200283 echo $this->table->generate();