blob: 9bc3f34231d1551097c5a3ee17aefd368a012846 [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);
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
120******************
121Function Reference
122******************
123
124$this->table->generate()
125========================
126
127Returns a string containing the generated table. Accepts an optional
128parameter which can be an array or a database result object.
129
130$this->table->set_caption()
131============================
132
133Permits you to add a caption to the table.
134
135::
136
137 $this->table->set_caption('Colors');
138
139$this->table->set_heading()
140============================
141
142Permits you to set the table heading. You can submit an array or
143discrete params::
144
145 $this->table->set_heading('Name', 'Color', 'Size');
146
147::
148
149 $this->table->set_heading(array('Name', 'Color', 'Size'));
150
151$this->table->add_row()
152========================
153
154Permits you to add a row to your table. You can submit an array or
155discrete params::
156
157 $this->table->add_row('Blue', 'Red', 'Green');
158
159::
160
161 $this->table->add_row(array('Blue', 'Red', 'Green'));
162
163If you would like to set an individual cell's tag attributes, you can
164use an associative array for that cell. The associative key 'data'
165defines the cell's data. Any other key => val pairs are added as
166key='val' attributes to the tag::
167
Derek Jonesd4678622011-10-05 15:40:24 -0500168 $cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);
169 $this->table->add_row($cell, 'Red', 'Green');
170
171 // generates
172 // <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
174$this->table->make_columns()
175=============================
176
177This function takes a one-dimensional array as input and creates a
178multi-dimensional array with a depth equal to the number of columns
179desired. This allows a single array with many elements to be displayed
180in a table that has a fixed column count. Consider this example::
181
Derek Jonesd4678622011-10-05 15:40:24 -0500182 $list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
183
184 $new_list = $this->table->make_columns($list, 3);
185
186 $this->table->generate($new_list);
187
188 // Generates a table with this prototype
189
190 <table border="0" cellpadding="4" cellspacing="0">
191 <tr>
192 <td>one</td><td>two</td><td>three</td>
193 </tr><tr>
194 <td>four</td><td>five</td><td>six</td>
195 </tr><tr>
196 <td>seven</td><td>eight</td><td>nine</td>
197 </tr><tr>
198 <td>ten</td><td>eleven</td><td>twelve</td></tr>
199 </table>
Derek Jones8ede1a22011-10-05 13:34:52 -0500200
201$this->table->set_template()
202=============================
203
204Permits you to set your template. You can submit a full or partial
205template.
206
207::
208
Derek Jonesd4678622011-10-05 15:40:24 -0500209 $tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
210
211 $this->table->set_template($tmpl);
Derek Jones8ede1a22011-10-05 13:34:52 -0500212
213$this->table->set_empty()
214==========================
215
216Let's you set a default value for use in any table cells that are empty.
217You might, for example, set a non-breaking space::
218
219 $this->table->set_empty("&nbsp;");
220
221$this->table->clear()
222=====================
223
224Lets you clear the table heading and row data. If you need to show
225multiple tables with different data you should to call this function
226after each table has been generated to empty the previous table
227information. Example::
228
Derek Jonesd4678622011-10-05 15:40:24 -0500229 $this->load->library('table');
230
231 $this->table->set_heading('Name', 'Color', 'Size');
232 $this->table->add_row('Fred', 'Blue', 'Small');
233 $this->table->add_row('Mary', 'Red', 'Large');
234 $this->table->add_row('John', 'Green', 'Medium');
235
236 echo $this->table->generate();
237
238 $this->table->clear();
239
240 $this->table->set_heading('Name', 'Day', 'Delivery');
241 $this->table->add_row('Fred', 'Wednesday', 'Express');
242 $this->table->add_row('Mary', 'Monday', 'Air');
243 $this->table->add_row('John', 'Saturday', 'Overnight');
244
245 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -0500246
247$this->table->function
248======================
249
250Allows you to specify a native PHP function or a valid function array
251object to be applied to all cell data.
252
253::
254
Derek Jonesd4678622011-10-05 15:40:24 -0500255 $this->load->library('table');
256
257 $this->table->set_heading('Name', 'Color', 'Size');
258 $this->table->add_row('Fred', '<strong>Blue</strong>', 'Small');
259
260 $this->table->function = 'htmlspecialchars';
261 echo $this->table->generate();
Derek Jones8ede1a22011-10-05 13:34:52 -0500262
263In the above example, all cell data would be ran through PHP's
264htmlspecialchars() function, resulting in::
265
266 <td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>
267