blob: 4ce948e60536903607fc6f63098d95ec23afdc07 [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
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : HTML Table Class</title>
admin9fd91c02006-10-09 03:30:36 +00006
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>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
admin9fd91c02006-10-09 03:30:36 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
admin9fd91c02006-10-09 03:30:36 +000021
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<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>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Allard60ca9b72007-07-12 19:53:27 +000031<td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
admin9fd91c02006-10-09 03:30:36 +000033</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000043<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
admin9fd91c02006-10-09 03:30:36 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
admina3962762006-10-09 03:31:32 +000045HTML Table Class
admin9fd91c02006-10-09 03:30:36 +000046</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="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>
admin9fd91c02006-10-09 03:30:36 +000048</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
admina3962762006-10-09 03:31:32 +000059<h1>HTML Table Class</h1>
admin9fd91c02006-10-09 03:30:36 +000060
admina3962762006-10-09 03:31:32 +000061<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 +000062
63<h2>Initializing the Class</h2>
64
Derek Allardd2df9bc2007-04-15 17:41:17 +000065<p>Like most other classes in CodeIgniter, the Table class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
admin9fd91c02006-10-09 03:30:36 +000066
admina3962762006-10-09 03:31:32 +000067<code>$this->load->library('table');</code>
admina1931ad2006-10-09 04:17:38 +000068<p>Once loaded, the Table library object will be available using: <dfn>$this->table</dfn></p>
admin9fd91c02006-10-09 03:30:36 +000069
70
admina1931ad2006-10-09 04:17:38 +000071<h2>Examples</h2>
72
admine334c472006-10-21 19:44:22 +000073<p>Here is an example showing how you can create a table from a multi-dimensional array.
74Note 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 +000075<dfn>set_heading()</dfn> function described in the function reference below).</p>
admina1931ad2006-10-09 04:17:38 +000076
77<code>
78$this->load->library('table');<br />
79<br />
80$data = array(<br />
81&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Name', 'Color', 'Size'),<br />
82&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Fred', 'Blue', 'Small'),<br />
83&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Mary', 'Red', 'Large'),<br />
84&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('John', 'Green', 'Medium') <br />
85&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
86<br />
87echo $this->table->generate($data);
88</code>
89
admine334c472006-10-21 19:44:22 +000090<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 +000091headings based on the table names (or you can set your own headings using the <dfn>set_heading()</dfn> function described
92in the function reference below).</p>
admina1931ad2006-10-09 04:17:38 +000093
94<code>
95$this->load->library('table');<br />
96<br />
97$query = $this->db->query("SELECT * FROM my_table");<br />
98<br />
99echo $this->table->generate($query);
100</code>
101
102
103<p>Here is an example showing how you might create a table using discreet parameters:</p>
104
105<code>
106$this->load->library('table');<br />
107<br />
108$this->table->set_heading('Name', 'Color', 'Size');<br />
109<br />
110$this->table->add_row('Fred', 'Blue', 'Small');<br />
111$this->table->add_row('Mary', 'Red', 'Large');<br />
112$this->table->add_row('John', 'Green', 'Medium');<br />
113<br />
114echo $this->table->generate();
115</code>
116
Derek Allardc6441282007-07-04 23:54:32 +0000117<p>Here is the same example, except instead of individual parameters, arrays are used:</p>
admina1931ad2006-10-09 04:17:38 +0000118
119<code>
120$this->load->library('table');<br />
121<br />
122$this->table->set_heading(array('Name', 'Color', 'Size'));<br />
123<br />
124$this->table->add_row(array('Fred', 'Blue', 'Small'));<br />
125$this->table->add_row(array('Mary', 'Red', 'Large'));<br />
126$this->table->add_row(array('John', 'Green', 'Medium'));<br />
127<br />
128echo $this->table->generate();
129</code>
130
131
132<h2>Changing the Look of Your Table</h2>
133
134<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
135prototype:</p>
136
137<code>
138$tmpl = array (<br />
139&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 />
140<br />
141&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 />
142&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 />
143&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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;=> '&lt;/table>'<br />
157&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
158
159<br />
160$this->table->set_template($tmpl);
161</code>
162
admine334c472006-10-21 19:44:22 +0000163<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 +0000164iteration of the row data.</p>
165
166<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.
167In this example, only the table opening tag is being changed:</p>
168
169<code>
170$tmpl = array ( 'table_open'&nbsp;&nbsp;=> '&lt;table border="1" cellpadding="2" cellspacing="1" class="mytable">' );<br />
171
172<br />
173$this->table->set_template($tmpl);
174</code>
175
176<br />
177<h1>Function Reference</h1>
178
179<h2>$this->table->generate()</h2>
180<p>Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.</p>
181
Derek Allardd66ba052007-01-27 17:44:16 +0000182<h2>$this->table->set_caption()</h2>
183
184<p>Permits you to add a caption to the table.</p>
185
Derek Allardace387a2007-02-01 20:15:32 +0000186<code>$this->table->set_caption('Colors');</code>
Derek Allardd66ba052007-01-27 17:44:16 +0000187
admina1931ad2006-10-09 04:17:38 +0000188<h2>$this->table->set_heading()</h2>
189
190<p>Permits you to set the table heading. You can submit an array or discreet params:</p>
191
192<code>$this->table->set_heading('Name', 'Color', 'Size');</code>
193<code>$this->table->set_heading(array('Name', 'Color', 'Size'));</code>
194
195<h2>$this->table->add_row()</h2>
196
197<p>Permits you to add a row to your table. You can submit an array or discreet params:</p>
198
199<code>$this->table->add_row('Blue', 'Red', 'Green');</code>
200<code>$this->table->add_row(array('Blue', 'Red', 'Green'));</code>
201
admin26364b92006-10-28 05:22:02 +0000202
203<h2>$this->table->make_columns()</h2>
204
205<p>This function takes a one-dimensional array as input and creates
206a multi-dimensional array with a depth equal to the number of
207columns desired. This allows a single array with many elements to be
208displayed in a table that has a fixed column count. Consider this example:</p>
209
210<code>
211$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');<br />
212<br />
213$new_list = $this->table->make_columns($list, 3);<br />
214<br />
215$this->table->generate($new_list)<br />
216<br />
217// Generates a table with this prototype<br />
218<br />
219&lt;table border="0" cellpadding="4" cellspacing="0"&gt;<br />
220&lt;tr&gt;<br />
221&lt;td&gt;one&lt;/td&gt;&lt;td&gt;two&lt;/td&gt;&lt;td&gt;three&lt;/td&gt;<br />
222&lt;/tr&gt;&lt;tr&gt;<br />
223&lt;td&gt;four&lt;/td&gt;&lt;td&gt;five&lt;/td&gt;&lt;td&gt;six&lt;/td&gt;<br />
224&lt;/tr&gt;&lt;tr&gt;<br />
225&lt;td&gt;seven&lt;/td&gt;&lt;td&gt;eight&lt;/td&gt;&lt;td&gt;nine&lt;/td&gt;<br />
226&lt;/tr&gt;&lt;tr&gt;<br />
227&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 />
228&lt;/table&gt;</code>
229
230
231
admina1931ad2006-10-09 04:17:38 +0000232<h2>$this->table->set_template()</h2>
233
234<p>Permits you to set your template. You can submit a full or partial template.</p>
235
236<code>
237$tmpl = array ( 'table_open'&nbsp;&nbsp;=> '&lt;table border="1" cellpadding="2" cellspacing="1" class="mytable">' );<br />
238
239<br />
240$this->table->set_template($tmpl);
241</code>
admin9fd91c02006-10-09 03:30:36 +0000242
243
admin2bc13852006-10-24 23:16:17 +0000244<h2>$this->table->set_empty()</h2>
245
246<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>
247
248<code>
249$this->table->set_empty("&amp;nbsp;");
250</code>
251
adminf3a62042006-10-29 20:24:11 +0000252<h2>$this->table->clear()</h2>
admin7af20d82006-10-28 19:08:57 +0000253
254<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 +0000255to call this function after each table has been generated to empty the previous table information. Example:</p>
admin2bc13852006-10-24 23:16:17 +0000256
adminf3a62042006-10-29 20:24:11 +0000257<code>
258$this->load->library('table');<br />
259<br />
260$this->table->set_heading('Name', 'Color', 'Size');<br />
261$this->table->add_row('Fred', 'Blue', 'Small');<br />
262$this->table->add_row('Mary', 'Red', 'Large');<br />
263$this->table->add_row('John', 'Green', 'Medium');<br />
264<br />
265echo $this->table->generate();<br />
266<br />
267<kbd>$this->table->clear();</kbd><br />
268<br />
269$this->table->set_heading('Name', 'Day', 'Delivery');<br />
270$this->table->add_row('Fred', 'Wednesday', 'Express');<br />
271$this->table->add_row('Mary', 'Monday', 'Air');<br />
272$this->table->add_row('John', 'Saturday', 'Overnight');<br />
273<br />
274echo $this->table->generate();
275</code>
admin2bc13852006-10-24 23:16:17 +0000276
admin9fd91c02006-10-09 03:30:36 +0000277</div>
278<!-- END CONTENT -->
279
280
281<div id="footer">
282<p>
Derek Allard9da4dbc2007-04-03 11:39:35 +0000283Previous Topic:&nbsp;<a href="ftp.html">&nbsp;FTP Class</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin9fd91c02006-10-09 03:30:36 +0000284<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>
Derek Allardc6441282007-07-04 23:54:32 +0000287</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000288<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
admin9fd91c02006-10-09 03:30:36 +0000289</div>
290
291</body>
292</html>