blob: 91ae1ae8dbd4e4112c16b620fa040cc9c37da3ea [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(
AdwinTrave7428ff02014-06-04 17:44:17 -050098 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
Derek Jonesd4678622011-10-05 15:40:24 -050099
AdwinTrave7428ff02014-06-04 17:44:17 -0500100 'thead_open' => '<thead>',
101 'thead_close' => '</thead>',
Derek Jonesd4678622011-10-05 15:40:24 -0500102
AdwinTrave7428ff02014-06-04 17:44:17 -0500103 'heading_row_start' => '<tr>',
104 'heading_row_end' => '</tr>',
105 'heading_cell_start' => '<th>',
106 'heading_cell_end' => '</th>',
Derek Jonesd4678622011-10-05 15:40:24 -0500107
AdwinTrave7428ff02014-06-04 17:44:17 -0500108 'tbody_open' => '<tbody>',
109 'tbody_close' => '</tbody>',
Derek Jonesd4678622011-10-05 15:40:24 -0500110
AdwinTrave7428ff02014-06-04 17:44:17 -0500111 'row_start' => '<tr>',
112 'row_end' => '</tr>',
113 'cell_start' => '<td>',
114 'cell_end' => '</td>',
115
116 'row_alt_start' => '<tr>',
117 'row_alt_end' => '</tr>',
118 'cell_alt_start' => '<td>',
119 'cell_alt_end' => '</td>',
120
121 'table_close' => '</table>'
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300122 );
Derek Jonesd4678622011-10-05 15:40:24 -0500123
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300124 $this->table->set_template($template);
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
126.. note:: You'll notice there are two sets of "row" blocks in the
127 template. These permit you to create alternating row colors or design
128 elements that alternate with each iteration of the row data.
129
130You are NOT required to submit a complete template. If you only need to
131change parts of the layout you can simply submit those elements. In this
132example, only the table opening tag is being changed::
133
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300134 $template = array(
135 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
136 );
Derek Jonesd4678622011-10-05 15:40:24 -0500137
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300138 $this->table->set_template($template);
Mike Funk46e3a9a2012-02-24 09:38:35 -0500139
140You can also set defaults for these in a config file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300142***************
143Class Reference
144***************
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200146.. php:class:: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Andrey Andreeva49c8ad2014-02-11 17:15:07 +0200148 .. attribute:: $function = NULL
Derek Jones8ede1a22011-10-05 13:34:52 -0500149
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300150 Allows you to specify a native PHP function or a valid function array object to be applied to all cell data.
151 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300153 $this->load->library('table');
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300155 $this->table->set_heading('Name', 'Color', 'Size');
156 $this->table->add_row('Fred', '<strong>Blue</strong>', 'Small');
Derek Jones8ede1a22011-10-05 13:34:52 -0500157
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300158 $this->table->function = 'htmlspecialchars';
159 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300161 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 -0500162
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300163 <td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200165 .. php:method:: generate([$table_data = NULL])
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
Andrey Andreev28c2c972014-02-08 04:27:48 +0200167 :param mixed $table_data: Data to populate the table rows with
168 :returns: HTML table
169 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300171 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 -0500172
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200173 .. php:method:: set_caption($caption)
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Andrey Andreev28c2c972014-02-08 04:27:48 +0200175 :param string $caption: Table caption
Andrey Andreev1f590952014-02-08 19:50:26 +0200176 :returns: CI_Table instance (method chaining)
177 :rtype: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500178
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300179 Permits you to add a caption to the table.
180 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500181
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300182 $this->table->set_caption('Colors');
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200184 .. php:method:: set_heading([$args = array()[, ...]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Andrey Andreev28c2c972014-02-08 04:27:48 +0200186 :param mixed $args: An array or multiple strings containing the table column titles
Andrey Andreev1f590952014-02-08 19:50:26 +0200187 :returns: CI_Table instance (method chaining)
188 :rtype: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500189
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300190 Permits you to set the table heading. You can submit an array or discrete params::
Derek Jonesd4678622011-10-05 15:40:24 -0500191
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300192 $this->table->set_heading('Name', 'Color', 'Size');
Derek Jones8ede1a22011-10-05 13:34:52 -0500193
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300194 $this->table->set_heading(array('Name', 'Color', 'Size'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500195
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200196 .. php:method:: add_row([$args = array()[, ...]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500197
Andrey Andreev28c2c972014-02-08 04:27:48 +0200198 :param mixed $args: An array or multiple strings containing the row values
Andrey Andreev1f590952014-02-08 19:50:26 +0200199 :returns: CI_Table instance (method chaining)
200 :rtype: CI_Table
Derek Jonesd4678622011-10-05 15:40:24 -0500201
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300202 Permits you to add a row to your table. You can submit an array or discrete params::
Derek Jonesd4678622011-10-05 15:40:24 -0500203
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300204 $this->table->add_row('Blue', 'Red', 'Green');
Derek Jonesd4678622011-10-05 15:40:24 -0500205
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300206 $this->table->add_row(array('Blue', 'Red', 'Green'));
Derek Jonesd4678622011-10-05 15:40:24 -0500207
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300208 If you would like to set an individual cell's tag attributes, you can use an associative array for that cell.
209 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 -0500210
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300211 $cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);
212 $this->table->add_row($cell, 'Red', 'Green');
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300214 // generates
215 // <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
Derek Jones8ede1a22011-10-05 13:34:52 -0500216
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200217 .. php:method:: make_columns([$array = array()[, $col_limit = 0]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500218
Andrey Andreev28c2c972014-02-08 04:27:48 +0200219 :param array $array: An array containing multiple rows' data
220 :param int $col_limit: Count of columns in the table
221 :returns: An array of HTML table columns
222 :rtype: array
Derek Jonesd4678622011-10-05 15:40:24 -0500223
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300224 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.
225 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 -0500226
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300227 $list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
Derek Jones8ede1a22011-10-05 13:34:52 -0500228
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300229 $new_list = $this->table->make_columns($list, 3);
Derek Jones8ede1a22011-10-05 13:34:52 -0500230
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300231 $this->table->generate($new_list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300233 // Generates a table with this prototype
Derek Jones8ede1a22011-10-05 13:34:52 -0500234
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300235 <table border="0" cellpadding="4" cellspacing="0">
236 <tr>
237 <td>one</td><td>two</td><td>three</td>
238 </tr><tr>
239 <td>four</td><td>five</td><td>six</td>
240 </tr><tr>
241 <td>seven</td><td>eight</td><td>nine</td>
242 </tr><tr>
243 <td>ten</td><td>eleven</td><td>twelve</td></tr>
244 </table>
Derek Jones8ede1a22011-10-05 13:34:52 -0500245
Derek Jonesd4678622011-10-05 15:40:24 -0500246
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200247 .. php:method:: set_template($template)
Derek Jonesd4678622011-10-05 15:40:24 -0500248
Andrey Andreev28c2c972014-02-08 04:27:48 +0200249 :param array $template: An associative array containing template values
250 :returns: TRUE on success, FALSE on failure
251 :rtype: bool
Derek Jonesd4678622011-10-05 15:40:24 -0500252
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300253 Permits you to set your template. You can submit a full or partial template.
254 ::
Derek Jonesd4678622011-10-05 15:40:24 -0500255
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600256 $template = array(
257 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
258 );
259
260 $this->table->set_template($template);
Derek Jones8ede1a22011-10-05 13:34:52 -0500261
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200262 .. php:method:: set_empty($value)
Derek Jones8ede1a22011-10-05 13:34:52 -0500263
Andrey Andreev28c2c972014-02-08 04:27:48 +0200264 :param mixed $value: Value to put in empty cells
Andrey Andreev1f590952014-02-08 19:50:26 +0200265 :returns: CI_Table instance (method chaining)
266 :rtype: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500267
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300268 Lets you set a default value for use in any table cells that are empty.
269 You might, for example, set a non-breaking space::
Derek Jones8ede1a22011-10-05 13:34:52 -0500270
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300271 $this->table->set_empty("&nbsp;");
Derek Jonesd4678622011-10-05 15:40:24 -0500272
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200273 .. php:method:: clear()
Derek Jonesd4678622011-10-05 15:40:24 -0500274
Andrey Andreev1f590952014-02-08 19:50:26 +0200275 :returns: CI_Table instance (method chaining)
276 :rtype: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500277
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300278 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
279 after each table has been generated to clear the previous table information. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500280
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300281 $this->load->library('table');
Derek Jones8ede1a22011-10-05 13:34:52 -0500282
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300283 $this->table->set_heading('Name', 'Color', 'Size');
284 $this->table->add_row('Fred', 'Blue', 'Small');
285 $this->table->add_row('Mary', 'Red', 'Large');
286 $this->table->add_row('John', 'Green', 'Medium');
287
288 echo $this->table->generate();
289
290 $this->table->clear();
291
292 $this->table->set_heading('Name', 'Day', 'Delivery');
293 $this->table->add_row('Fred', 'Wednesday', 'Express');
294 $this->table->add_row('Mary', 'Monday', 'Air');
295 $this->table->add_row('John', 'Saturday', 'Overnight');
296
AdwinTrave7428ff02014-06-04 17:44:17 -0500297 echo $this->table->generate();