blob: 6a808abc26d557ccf6ab3d798074dbbda57a78b8 [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
8Initializing the Class
9======================
10
11Like most other classes in CodeIgniter, the Table class is initialized
12in your controller using the $this->load->library function::
13
14 $this->load->library('table');
15
16Once loaded, the Table library object will be available using:
17$this->table
18
19Examples
20========
21
22Here is an example showing how you can create a table from a
23multi-dimensional array. Note that the first array index will become the
24table heading (or you can set your own headings using the set_heading()
25function described in the function reference below).
26
27::
28
Derek Jonesd4678622011-10-05 15:40:24 -050029 $this->load->library('table');
30
31 $data = array(
32 array('Name', 'Color', 'Size'),
33 array('Fred', 'Blue', 'Small'),
34 array('Mary', 'Red', 'Large'),
35 array('John', 'Green', 'Medium')
36 );
37
38 echo $this->table->generate($data);
Derek Jones8ede1a22011-10-05 13:34:52 -050039
40Here is an example of a table created from a database query result. The
41table class will automatically generate the headings based on the table
42names (or you can set your own headings using the set_heading()
43function described in the function reference below).
44
45::
46
Derek Jonesd4678622011-10-05 15:40:24 -050047 $this->load->library('table');
48
49 $query = $this->db->query("SELECT * FROM my_table");
50
51 echo $this->table->generate($query);
Derek Jones8ede1a22011-10-05 13:34:52 -050052
53Here is an example showing how you might create a table using discrete
54parameters::
55
Derek Jonesd4678622011-10-05 15:40:24 -050056 $this->load->library('table');
57
58 $this->table->set_heading('Name', 'Color', 'Size');
59
60 $this->table->add_row('Fred', 'Blue', 'Small');
61 $this->table->add_row('Mary', 'Red', 'Large');
62 $this->table->add_row('John', 'Green', 'Medium');
63
64 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -050065
66Here is the same example, except instead of individual parameters,
67arrays are used::
68
Derek Jonesd4678622011-10-05 15:40:24 -050069 $this->load->library('table');
70
71 $this->table->set_heading(array('Name', 'Color', 'Size'));
72
73 $this->table->add_row(array('Fred', 'Blue', 'Small'));
74 $this->table->add_row(array('Mary', 'Red', 'Large'));
75 $this->table->add_row(array('John', 'Green', 'Medium'));
76
77 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -050078
79Changing the Look of Your Table
80===============================
81
82The Table Class permits you to set a table template with which you can
83specify the design of your layout. Here is the template prototype::
84
Derek Jonesd4678622011-10-05 15:40:24 -050085 $tmpl = array (
86 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
87
88 'heading_row_start' => '<tr>',
89 'heading_row_end' => '</tr>',
90 'heading_cell_start' => '<th>',
91 'heading_cell_end' => '</th>',
92
93 'row_start' => '<tr>',
94 'row_end' => '</tr>',
95 'cell_start' => '<td>',
96 'cell_end' => '</td>',
97
98 'row_alt_start' => '<tr>',
99 'row_alt_end' => '</tr>',
100 'cell_alt_start' => '<td>',
101 'cell_alt_end' => '</td>',
102
103 'table_close' => '</table>'
104 );
105
106 $this->table->set_template($tmpl);
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
108.. note:: You'll notice there are two sets of "row" blocks in the
109 template. These permit you to create alternating row colors or design
110 elements that alternate with each iteration of the row data.
111
112You are NOT required to submit a complete template. If you only need to
113change parts of the layout you can simply submit those elements. In this
114example, only the table opening tag is being changed::
115
Derek Jonesd4678622011-10-05 15:40:24 -0500116 $tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
117
118 $this->table->set_template($tmpl);
Mike Funk46e3a9a2012-02-24 09:38:35 -0500119
120You can also set defaults for these in a config file.
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
122******************
123Function Reference
124******************
125
126$this->table->generate()
127========================
128
129Returns a string containing the generated table. Accepts an optional
130parameter which can be an array or a database result object.
131
132$this->table->set_caption()
133============================
134
135Permits you to add a caption to the table.
136
137::
138
139 $this->table->set_caption('Colors');
140
141$this->table->set_heading()
142============================
143
144Permits you to set the table heading. You can submit an array or
145discrete params::
146
147 $this->table->set_heading('Name', 'Color', 'Size');
148
149::
150
151 $this->table->set_heading(array('Name', 'Color', 'Size'));
152
153$this->table->add_row()
154========================
155
156Permits you to add a row to your table. You can submit an array or
157discrete params::
158
159 $this->table->add_row('Blue', 'Red', 'Green');
160
161::
162
163 $this->table->add_row(array('Blue', 'Red', 'Green'));
164
165If you would like to set an individual cell's tag attributes, you can
166use an associative array for that cell. The associative key 'data'
167defines the cell's data. Any other key => val pairs are added as
168key='val' attributes to the tag::
169
Derek Jonesd4678622011-10-05 15:40:24 -0500170 $cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);
171 $this->table->add_row($cell, 'Red', 'Green');
172
173 // generates
174 // <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
176$this->table->make_columns()
177=============================
178
179This function takes a one-dimensional array as input and creates a
180multi-dimensional array with a depth equal to the number of columns
181desired. This allows a single array with many elements to be displayed
182in a table that has a fixed column count. Consider this example::
183
Derek Jonesd4678622011-10-05 15:40:24 -0500184 $list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
185
186 $new_list = $this->table->make_columns($list, 3);
187
188 $this->table->generate($new_list);
189
190 // Generates a table with this prototype
191
192 <table border="0" cellpadding="4" cellspacing="0">
193 <tr>
194 <td>one</td><td>two</td><td>three</td>
195 </tr><tr>
196 <td>four</td><td>five</td><td>six</td>
197 </tr><tr>
198 <td>seven</td><td>eight</td><td>nine</td>
199 </tr><tr>
200 <td>ten</td><td>eleven</td><td>twelve</td></tr>
201 </table>
Derek Jones8ede1a22011-10-05 13:34:52 -0500202
203$this->table->set_template()
204=============================
205
206Permits you to set your template. You can submit a full or partial
207template.
208
209::
210
Derek Jonesd4678622011-10-05 15:40:24 -0500211 $tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
212
213 $this->table->set_template($tmpl);
Derek Jones8ede1a22011-10-05 13:34:52 -0500214
215$this->table->set_empty()
216==========================
217
218Let's you set a default value for use in any table cells that are empty.
219You might, for example, set a non-breaking space::
220
221 $this->table->set_empty("&nbsp;");
222
223$this->table->clear()
224=====================
225
226Lets you clear the table heading and row data. If you need to show
227multiple tables with different data you should to call this function
228after each table has been generated to empty the previous table
229information. Example::
230
Derek Jonesd4678622011-10-05 15:40:24 -0500231 $this->load->library('table');
232
233 $this->table->set_heading('Name', 'Color', 'Size');
234 $this->table->add_row('Fred', 'Blue', 'Small');
235 $this->table->add_row('Mary', 'Red', 'Large');
236 $this->table->add_row('John', 'Green', 'Medium');
237
238 echo $this->table->generate();
239
240 $this->table->clear();
241
242 $this->table->set_heading('Name', 'Day', 'Delivery');
243 $this->table->add_row('Fred', 'Wednesday', 'Express');
244 $this->table->add_row('Mary', 'Monday', 'Air');
245 $this->table->add_row('John', 'Saturday', 'Overnight');
246
247 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -0500248
249$this->table->function
250======================
251
252Allows you to specify a native PHP function or a valid function array
253object to be applied to all cell data.
254
255::
256
Derek Jonesd4678622011-10-05 15:40:24 -0500257 $this->load->library('table');
258
259 $this->table->set_heading('Name', 'Color', 'Size');
260 $this->table->add_row('Fred', '<strong>Blue</strong>', 'Small');
261
262 $this->table->function = 'htmlspecialchars';
263 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -0500264
265In the above example, all cell data would be ran through PHP's
266htmlspecialchars() function, resulting in::
267
268 <td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>
269