blob: f62d27807c3bdde5f6fcc2b38f3eef2875e4807a [file] [log] [blame]
admin9fd91c02006-10-09 03:30:36 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
5<title>Code Igniter User Guide</title>
6
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
10<script type="text/javascript" src="../nav/nav.js"></script>
11<script type="text/javascript" src="../nav/prototype.lite.js"></script>
12<script type="text/javascript" src="../nav/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
admin9fd91c02006-10-09 03:30:36 +000016 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
25<meta name='description' content='Code Igniter User Guide' />
26
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
Derek Allardf743c732007-02-14 01:36:46 +000036<td><h1>Code Igniter User Guide Version 1.5.2</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
admin9fd91c02006-10-09 03:30:36 +000038</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
admina3962762006-10-09 03:31:32 +000050HTML Table Class
admin9fd91c02006-10-09 03:30:36 +000051</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
admina3962762006-10-09 03:31:32 +000064<h1>HTML Table Class</h1>
admin9fd91c02006-10-09 03:30:36 +000065
admina3962762006-10-09 03:31:32 +000066<p>The Table Class provides functions that enable you to auto-generate HTML tables from arrays or database result sets.</p>
admin9fd91c02006-10-09 03:30:36 +000067
68<h2>Initializing the Class</h2>
69
admina3962762006-10-09 03:31:32 +000070<p>Like most other classes in Code Igniter, the Table class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
admin9fd91c02006-10-09 03:30:36 +000071
admina3962762006-10-09 03:31:32 +000072<code>$this->load->library('table');</code>
admina1931ad2006-10-09 04:17:38 +000073<p>Once loaded, the Table library object will be available using: <dfn>$this->table</dfn></p>
admin9fd91c02006-10-09 03:30:36 +000074
75
admina1931ad2006-10-09 04:17:38 +000076<h2>Examples</h2>
77
admine334c472006-10-21 19:44:22 +000078<p>Here is an example showing how you can create a table from a multi-dimensional array.
79Note that the first array index will become the table heading (or you can set your own headings using the
admin2f0bac82006-10-09 17:36:45 +000080<dfn>set_heading()</dfn> function described in the function reference below).</p>
admina1931ad2006-10-09 04:17:38 +000081
82<code>
83$this->load->library('table');<br />
84<br />
85$data = array(<br />
86&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Name', 'Color', 'Size'),<br />
87&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Fred', 'Blue', 'Small'),<br />
88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Mary', 'Red', 'Large'),<br />
89&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('John', 'Green', 'Medium') <br />
90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
91<br />
92echo $this->table->generate($data);
93</code>
94
admine334c472006-10-21 19:44:22 +000095<p>Here is an example of a table created from a database query result. The table class will automatically generate the
admin2f0bac82006-10-09 17:36:45 +000096headings based on the table names (or you can set your own headings using the <dfn>set_heading()</dfn> function described
97in the function reference below).</p>
admina1931ad2006-10-09 04:17:38 +000098
99<code>
100$this->load->library('table');<br />
101<br />
102$query = $this->db->query("SELECT * FROM my_table");<br />
103<br />
104echo $this->table->generate($query);
105</code>
106
107
108<p>Here is an example showing how you might create a table using discreet parameters:</p>
109
110<code>
111$this->load->library('table');<br />
112<br />
113$this->table->set_heading('Name', 'Color', 'Size');<br />
114<br />
115$this->table->add_row('Fred', 'Blue', 'Small');<br />
116$this->table->add_row('Mary', 'Red', 'Large');<br />
117$this->table->add_row('John', 'Green', 'Medium');<br />
118<br />
119echo $this->table->generate();
120</code>
121
122<p>Here is the same example, except instead of individual parameters, arrays are used:
123
124<code>
125$this->load->library('table');<br />
126<br />
127$this->table->set_heading(array('Name', 'Color', 'Size'));<br />
128<br />
129$this->table->add_row(array('Fred', 'Blue', 'Small'));<br />
130$this->table->add_row(array('Mary', 'Red', 'Large'));<br />
131$this->table->add_row(array('John', 'Green', 'Medium'));<br />
132<br />
133echo $this->table->generate();
134</code>
135
136
137<h2>Changing the Look of Your Table</h2>
138
139<p>The Table Class permits you to set a table template with which you can specify the design of your layout. Here is the template
140prototype:</p>
141
142<code>
143$tmpl = array (<br />
144&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'table_open'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '&lt;table border="0" cellpadding="4" cellspacing="0">',<br />
145<br />
146&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'heading_row_start'&nbsp;&nbsp;&nbsp;=> '&lt;tr>',<br />
147&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'heading_row_end'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '&lt;/tr>',<br />
148&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'heading_cell_start'&nbsp;&nbsp;=> '&lt;th>',<br />
149&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'heading_cell_end'&nbsp;&nbsp;&n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&nbsp;&nbsp;&nbsp;&nbsp;=> '&lt;tr>',<br />
157&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'row_alt_end'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '&lt;/tr>',<br />
158&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'cell_alt_start'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '&lt;td>',<br />
159&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'cell_alt_end'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '&lt;/td>',<br />
160<br />
161&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'table_close'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '&lt;/table>'<br />
162&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
163
164<br />
165$this->table->set_template($tmpl);
166</code>
167
admine334c472006-10-21 19:44:22 +0000168<p class="important"><strong>Note:</strong>&nbsp; You'll notice there are two sets of "row" blocks in the template. These permit you to create alternating row colors or design elements that alternate with each
admina1931ad2006-10-09 04:17:38 +0000169iteration of the row data.</p>
170
171<p>You are NOT required to submit a complete template. If you only need to change parts of the layout you can simply submit those elements.
172In this example, only the table opening tag is being changed:</p>
173
174<code>
175$tmpl = array ( 'table_open'&nbsp;&nbsp;=> '&lt;table border="1" cellpadding="2" cellspacing="1" class="mytable">' );<br />
176
177<br />
178$this->table->set_template($tmpl);
179</code>
180
181<br />
182<h1>Function Reference</h1>
183
184<h2>$this->table->generate()</h2>
185<p>Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.</p>
186
Derek Allardd66ba052007-01-27 17:44:16 +0000187<h2>$this->table->set_caption()</h2>
188
189<p>Permits you to add a caption to the table.</p>
190
Derek Allardace387a2007-02-01 20:15:32 +0000191<code>$this->table->set_caption('Colors');</code>
Derek Allardd66ba052007-01-27 17:44:16 +0000192
admina1931ad2006-10-09 04:17:38 +0000193<h2>$this->table->set_heading()</h2>
194
195<p>Permits you to set the table heading. You can submit an array or discreet params:</p>
196
197<code>$this->table->set_heading('Name', 'Color', 'Size');</code>
198<code>$this->table->set_heading(array('Name', 'Color', 'Size'));</code>
199
200<h2>$this->table->add_row()</h2>
201
202<p>Permits you to add a row to your table. You can submit an array or discreet params:</p>
203
204<code>$this->table->add_row('Blue', 'Red', 'Green');</code>
205<code>$this->table->add_row(array('Blue', 'Red', 'Green'));</code>
206
admin26364b92006-10-28 05:22:02 +0000207
208<h2>$this->table->make_columns()</h2>
209
210<p>This function takes a one-dimensional array as input and creates
211a multi-dimensional array with a depth equal to the number of
212columns desired. This allows a single array with many elements to be
213displayed in a table that has a fixed column count. Consider this example:</p>
214
215<code>
216$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');<br />
217<br />
218$new_list = $this->table->make_columns($list, 3);<br />
219<br />
220$this->table->generate($new_list)<br />
221<br />
222// Generates a table with this prototype<br />
223<br />
224&lt;table border="0" cellpadding="4" cellspacing="0"&gt;<br />
225&lt;tr&gt;<br />
226&lt;td&gt;one&lt;/td&gt;&lt;td&gt;two&lt;/td&gt;&lt;td&gt;three&lt;/td&gt;<br />
227&lt;/tr&gt;&lt;tr&gt;<br />
228&lt;td&gt;four&lt;/td&gt;&lt;td&gt;five&lt;/td&gt;&lt;td&gt;six&lt;/td&gt;<br />
229&lt;/tr&gt;&lt;tr&gt;<br />
230&lt;td&gt;seven&lt;/td&gt;&lt;td&gt;eight&lt;/td&gt;&lt;td&gt;nine&lt;/td&gt;<br />
231&lt;/tr&gt;&lt;tr&gt;<br />
232&lt;td&gt;ten&lt;/td&gt;&lt;td&gt;eleven&lt;/td&gt;&lt;td&gt;twelve&lt;/td&gt;&lt;/tr&gt;<br />
233&lt;/table&gt;</code>
234
235
236
admina1931ad2006-10-09 04:17:38 +0000237<h2>$this->table->set_template()</h2>
238
239<p>Permits you to set your template. You can submit a full or partial template.</p>
240
241<code>
242$tmpl = array ( 'table_open'&nbsp;&nbsp;=> '&lt;table border="1" cellpadding="2" cellspacing="1" class="mytable">' );<br />
243
244<br />
245$this->table->set_template($tmpl);
246</code>
admin9fd91c02006-10-09 03:30:36 +0000247
248
admin2bc13852006-10-24 23:16:17 +0000249<h2>$this->table->set_empty()</h2>
250
251<p>Let's you set a default value for use in any table cells that are empty. You might, for example, set a non-breaking space:</p>
252
253<code>
254$this->table->set_empty("&amp;nbsp;");
255</code>
256
adminf3a62042006-10-29 20:24:11 +0000257<h2>$this->table->clear()</h2>
admin7af20d82006-10-28 19:08:57 +0000258
259<p>Lets you clear the table heading and row data. If you need to show multiple tables with different data you should
adminf3a62042006-10-29 20:24:11 +0000260to call this function after each table has been generated to empty the previous table information. Example:</p>
admin2bc13852006-10-24 23:16:17 +0000261
adminf3a62042006-10-29 20:24:11 +0000262<code>
263$this->load->library('table');<br />
264<br />
265$this->table->set_heading('Name', 'Color', 'Size');<br />
266$this->table->add_row('Fred', 'Blue', 'Small');<br />
267$this->table->add_row('Mary', 'Red', 'Large');<br />
268$this->table->add_row('John', 'Green', 'Medium');<br />
269<br />
270echo $this->table->generate();<br />
271<br />
272<kbd>$this->table->clear();</kbd><br />
273<br />
274$this->table->set_heading('Name', 'Day', 'Delivery');<br />
275$this->table->add_row('Fred', 'Wednesday', 'Express');<br />
276$this->table->add_row('Mary', 'Monday', 'Air');<br />
277$this->table->add_row('John', 'Saturday', 'Overnight');<br />
278<br />
279echo $this->table->generate();
280</code>
admin2bc13852006-10-24 23:16:17 +0000281
admin9fd91c02006-10-09 03:30:36 +0000282</div>
283<!-- END CONTENT -->
284
285
286<div id="footer">
287<p>
admin40037182006-10-11 19:16:58 +0000288Previous Topic:&nbsp;&nbsp;<a href="file_uploading.html">File Uploading Class</a>
admin9fd91c02006-10-09 03:30:36 +0000289&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
290<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
291<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin40037182006-10-11 19:16:58 +0000292Next Topic:&nbsp;&nbsp;<a href="image_lib.html">Image Manipulation Class</a>
admin9fd91c02006-10-09 03:30:36 +0000293<p>
294<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
295</div>
296
297</body>
298</html>