blob: 9d95eddfcdb5dc7bf47938fd67139cb54ddb625e [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 Andreeva49c8ad2014-02-11 17:15:07 +0200142 .. attribute:: $function = NULL
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 Andreev28c2c972014-02-08 04:27:48 +0200161 :param mixed $table_data: Data to populate the table rows with
162 :returns: HTML table
163 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300165 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 -0500166
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300167 .. method:: set_caption($caption)
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Andrey Andreev28c2c972014-02-08 04:27:48 +0200169 :param string $caption: Table caption
Andrey Andreev1f590952014-02-08 19:50:26 +0200170 :returns: CI_Table instance (method chaining)
171 :rtype: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300173 Permits you to add a caption to the table.
174 ::
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300176 $this->table->set_caption('Colors');
Derek Jones8ede1a22011-10-05 13:34:52 -0500177
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300178 .. method:: set_heading([$args = array()[, ...]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreev28c2c972014-02-08 04:27:48 +0200180 :param mixed $args: An array or multiple strings containing the table column titles
Andrey Andreev1f590952014-02-08 19:50:26 +0200181 :returns: CI_Table instance (method chaining)
182 :rtype: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500183
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300184 Permits you to set the table heading. You can submit an array or discrete params::
Derek Jonesd4678622011-10-05 15:40:24 -0500185
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300186 $this->table->set_heading('Name', 'Color', 'Size');
Derek Jones8ede1a22011-10-05 13:34:52 -0500187
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300188 $this->table->set_heading(array('Name', 'Color', 'Size'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500189
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300190 .. method:: add_row([$args = array()[, ...]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500191
Andrey Andreev28c2c972014-02-08 04:27:48 +0200192 :param mixed $args: An array or multiple strings containing the row values
Andrey Andreev1f590952014-02-08 19:50:26 +0200193 :returns: CI_Table instance (method chaining)
194 :rtype: CI_Table
Derek Jonesd4678622011-10-05 15:40:24 -0500195
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300196 Permits you to add a row to your table. You can submit an array or discrete params::
Derek Jonesd4678622011-10-05 15:40:24 -0500197
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300198 $this->table->add_row('Blue', 'Red', 'Green');
Derek Jonesd4678622011-10-05 15:40:24 -0500199
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300200 $this->table->add_row(array('Blue', 'Red', 'Green'));
Derek Jonesd4678622011-10-05 15:40:24 -0500201
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300202 If you would like to set an individual cell's tag attributes, you can use an associative array for that cell.
203 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 -0500204
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300205 $cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);
206 $this->table->add_row($cell, 'Red', 'Green');
Derek Jones8ede1a22011-10-05 13:34:52 -0500207
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300208 // generates
209 // <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
Derek Jones8ede1a22011-10-05 13:34:52 -0500210
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300211 .. method:: make_columns([$array = array()[, $col_limit = 0]])
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
Andrey Andreev28c2c972014-02-08 04:27:48 +0200213 :param array $array: An array containing multiple rows' data
214 :param int $col_limit: Count of columns in the table
215 :returns: An array of HTML table columns
216 :rtype: array
Derek Jonesd4678622011-10-05 15:40:24 -0500217
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300218 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.
219 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 -0500220
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300221 $list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
Derek Jones8ede1a22011-10-05 13:34:52 -0500222
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300223 $new_list = $this->table->make_columns($list, 3);
Derek Jones8ede1a22011-10-05 13:34:52 -0500224
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300225 $this->table->generate($new_list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500226
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300227 // Generates a table with this prototype
Derek Jones8ede1a22011-10-05 13:34:52 -0500228
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300229 <table border="0" cellpadding="4" cellspacing="0">
230 <tr>
231 <td>one</td><td>two</td><td>three</td>
232 </tr><tr>
233 <td>four</td><td>five</td><td>six</td>
234 </tr><tr>
235 <td>seven</td><td>eight</td><td>nine</td>
236 </tr><tr>
237 <td>ten</td><td>eleven</td><td>twelve</td></tr>
238 </table>
Derek Jones8ede1a22011-10-05 13:34:52 -0500239
Derek Jonesd4678622011-10-05 15:40:24 -0500240
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300241 .. method:: set_template($template)
Derek Jonesd4678622011-10-05 15:40:24 -0500242
Andrey Andreev28c2c972014-02-08 04:27:48 +0200243 :param array $template: An associative array containing template values
244 :returns: TRUE on success, FALSE on failure
245 :rtype: bool
Derek Jonesd4678622011-10-05 15:40:24 -0500246
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300247 Permits you to set your template. You can submit a full or partial template.
248 ::
Derek Jonesd4678622011-10-05 15:40:24 -0500249
Connor Tumbleson75b3fb22014-01-11 06:58:43 -0600250 $template = array(
251 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
252 );
253
254 $this->table->set_template($template);
Derek Jones8ede1a22011-10-05 13:34:52 -0500255
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300256 .. method:: set_empty($value)
Derek Jones8ede1a22011-10-05 13:34:52 -0500257
Andrey Andreev28c2c972014-02-08 04:27:48 +0200258 :param mixed $value: Value to put in empty cells
Andrey Andreev1f590952014-02-08 19:50:26 +0200259 :returns: CI_Table instance (method chaining)
260 :rtype: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500261
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300262 Lets you set a default value for use in any table cells that are empty.
263 You might, for example, set a non-breaking space::
Derek Jones8ede1a22011-10-05 13:34:52 -0500264
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300265 $this->table->set_empty("&nbsp;");
Derek Jonesd4678622011-10-05 15:40:24 -0500266
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300267 .. method:: clear()
Derek Jonesd4678622011-10-05 15:40:24 -0500268
Andrey Andreev1f590952014-02-08 19:50:26 +0200269 :returns: CI_Table instance (method chaining)
270 :rtype: CI_Table
Derek Jones8ede1a22011-10-05 13:34:52 -0500271
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300272 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
273 after each table has been generated to clear the previous table information. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500274
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300275 $this->load->library('table');
Derek Jones8ede1a22011-10-05 13:34:52 -0500276
Andrey Andreev0ee2a632013-09-23 18:55:38 +0300277 $this->table->set_heading('Name', 'Color', 'Size');
278 $this->table->add_row('Fred', 'Blue', 'Small');
279 $this->table->add_row('Mary', 'Red', 'Large');
280 $this->table->add_row('John', 'Green', 'Medium');
281
282 echo $this->table->generate();
283
284 $this->table->clear();
285
286 $this->table->set_heading('Name', 'Day', 'Delivery');
287 $this->table->add_row('Fred', 'Wednesday', 'Express');
288 $this->table->add_row('Mary', 'Monday', 'Air');
289 $this->table->add_row('John', 'Saturday', 'Overnight');
290
Andrey Andreevcc042092014-01-03 17:08:27 +0200291 echo $this->table->generate();