blob: fe2f0c29f7ca21d0710c9360f7a0c2ee54e8cf31 [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>
admin05186612006-10-28 03:28:01 +000036<td><h1>Code Igniter User Guide Version 1.5.0</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;&nb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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
187<h2>$this->table->set_heading()</h2>
188
189<p>Permits you to set the table heading. You can submit an array or discreet params:</p>
190
191<code>$this->table->set_heading('Name', 'Color', 'Size');</code>
192<code>$this->table->set_heading(array('Name', 'Color', 'Size'));</code>
193
194<h2>$this->table->add_row()</h2>
195
196<p>Permits you to add a row to your table. You can submit an array or discreet params:</p>
197
198<code>$this->table->add_row('Blue', 'Red', 'Green');</code>
199<code>$this->table->add_row(array('Blue', 'Red', 'Green'));</code>
200
admin26364b92006-10-28 05:22:02 +0000201
202<h2>$this->table->make_columns()</h2>
203
204<p>This function takes a one-dimensional array as input and creates
205a multi-dimensional array with a depth equal to the number of
206columns desired. This allows a single array with many elements to be
207displayed in a table that has a fixed column count. Consider this example:</p>
208
209<code>
210$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');<br />
211<br />
212$new_list = $this->table->make_columns($list, 3);<br />
213<br />
214$this->table->generate($new_list)<br />
215<br />
216// Generates a table with this prototype<br />
217<br />
218&lt;table border="0" cellpadding="4" cellspacing="0"&gt;<br />
219&lt;tr&gt;<br />
220&lt;td&gt;one&lt;/td&gt;&lt;td&gt;two&lt;/td&gt;&lt;td&gt;three&lt;/td&gt;<br />
221&lt;/tr&gt;&lt;tr&gt;<br />
222&lt;td&gt;four&lt;/td&gt;&lt;td&gt;five&lt;/td&gt;&lt;td&gt;six&lt;/td&gt;<br />
223&lt;/tr&gt;&lt;tr&gt;<br />
224&lt;td&gt;seven&lt;/td&gt;&lt;td&gt;eight&lt;/td&gt;&lt;td&gt;nine&lt;/td&gt;<br />
225&lt;/tr&gt;&lt;tr&gt;<br />
226&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 />
227&lt;/table&gt;</code>
228
229
230
admina1931ad2006-10-09 04:17:38 +0000231<h2>$this->table->set_template()</h2>
232
233<p>Permits you to set your template. You can submit a full or partial template.</p>
234
235<code>
236$tmpl = array ( 'table_open'&nbsp;&nbsp;=> '&lt;table border="1" cellpadding="2" cellspacing="1" class="mytable">' );<br />
237
238<br />
239$this->table->set_template($tmpl);
240</code>
admin9fd91c02006-10-09 03:30:36 +0000241
242
admin2bc13852006-10-24 23:16:17 +0000243<h2>$this->table->set_empty()</h2>
244
245<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>
246
247<code>
248$this->table->set_empty("&amp;nbsp;");
249</code>
250
adminf3a62042006-10-29 20:24:11 +0000251<h2>$this->table->clear()</h2>
admin7af20d82006-10-28 19:08:57 +0000252
253<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 +0000254to call this function after each table has been generated to empty the previous table information. Example:</p>
admin2bc13852006-10-24 23:16:17 +0000255
adminf3a62042006-10-29 20:24:11 +0000256<code>
257$this->load->library('table');<br />
258<br />
259$this->table->set_heading('Name', 'Color', 'Size');<br />
260$this->table->add_row('Fred', 'Blue', 'Small');<br />
261$this->table->add_row('Mary', 'Red', 'Large');<br />
262$this->table->add_row('John', 'Green', 'Medium');<br />
263<br />
264echo $this->table->generate();<br />
265<br />
266<kbd>$this->table->clear();</kbd><br />
267<br />
268$this->table->set_heading('Name', 'Day', 'Delivery');<br />
269$this->table->add_row('Fred', 'Wednesday', 'Express');<br />
270$this->table->add_row('Mary', 'Monday', 'Air');<br />
271$this->table->add_row('John', 'Saturday', 'Overnight');<br />
272<br />
273echo $this->table->generate();
274</code>
admin2bc13852006-10-24 23:16:17 +0000275
admin9fd91c02006-10-09 03:30:36 +0000276</div>
277<!-- END CONTENT -->
278
279
280<div id="footer">
281<p>
admin40037182006-10-11 19:16:58 +0000282Previous Topic:&nbsp;&nbsp;<a href="file_uploading.html">File Uploading Class</a>
admin9fd91c02006-10-09 03:30:36 +0000283&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
284<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
285<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin40037182006-10-11 19:16:58 +0000286Next Topic:&nbsp;&nbsp;<a href="image_lib.html">Image Manipulation Class</a>
admin9fd91c02006-10-09 03:30:36 +0000287<p>
288<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>
289</div>
290
291</body>
292</html>