blob: 772e9582970b8086d4b63d0a83a12756858f5c5c [file] [log] [blame]
Derek Allard39b622d2008-01-16 21:10:09 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
Derek Allard39b622d2008-01-16 21:10:09 +00003<head>
4
Derek Allard8039d4c2008-05-31 02:47:56 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Derek Jonesfd93d222008-05-06 15:18:50 +00006<title>HTML Table Class : CodeIgniter User Guide</title>
Derek Allard39b622d2008-01-16 21:10:09 +00007
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11<script type="text/javascript" src="../nav/nav.js"></script>
12<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13<script type="text/javascript" src="../nav/moo.fx.js"></script>
14<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
Derek Allard39b622d2008-01-16 21:10:09 +000016<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allard39b622d2008-01-16 21:10:09 +000020<meta name='description' content='CodeIgniter User Guide' />
21
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 Jones1ca3fc42008-06-27 00:19:33 +000031<td><h1>CodeIgniter User Guide Version 1.6.3</h1></td>
Derek Allard39b622d2008-01-16 21:10:09 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33</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 Jones7a9193a2008-01-21 18:39:20 +000043<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
Derek Allard39b622d2008-01-16 21:10:09 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45HTML Table Class
46</td>
47<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>
48</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58
59<h1>HTML Table Class</h1>
60
61<p>The Table Class provides functions that enable you to auto-generate HTML tables from arrays or database result sets.</p>
62
63<h2>Initializing the Class</h2>
64
65<p>Like most other classes in CodeIgniter, the Table class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
66
67<code>$this->load->library('table');</code>
68<p>Once loaded, the Table library object will be available using: <dfn>$this->table</dfn></p>
69
70
71<h2>Examples</h2>
72
73<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
75<dfn>set_heading()</dfn> function described in the function reference below).</p>
76
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
90<p>Here is an example of a table created from a database query result. The table class will automatically generate the
91headings 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>
93
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
Derek Allard78729d82008-02-01 16:58:38 +0000103<p>Here is an example showing how you might create a table using discrete parameters:</p>
Derek Allard39b622d2008-01-16 21:10:09 +0000104
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
117<p>Here is the same example, except instead of individual parameters, arrays are used:</p>
118
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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'heading_cell_start'&nbsp;&nbsp;=> '&lt;th>',<br />
144&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;&nbsp;&nbsp;=> '&lt;/th>',<br />
145<br />
146&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'row_alt_end'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '&lt;/tr>',<br />
153&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 />
154&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 />
155<br />
156&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 />
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
163<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
164iteration 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
182<h2>$this->table->set_caption()</h2>
183
184<p>Permits you to add a caption to the table.</p>
185
186<code>$this->table->set_caption('Colors');</code>
187
188<h2>$this->table->set_heading()</h2>
189
Derek Allard78729d82008-02-01 16:58:38 +0000190<p>Permits you to set the table heading. You can submit an array or discrete params:</p>
Derek Allard39b622d2008-01-16 21:10:09 +0000191
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
Derek Allard78729d82008-02-01 16:58:38 +0000197<p>Permits you to add a row to your table. You can submit an array or discrete params:</p>
Derek Allard39b622d2008-01-16 21:10:09 +0000198
199<code>$this->table->add_row('Blue', 'Red', 'Green');</code>
200<code>$this->table->add_row(array('Blue', 'Red', 'Green'));</code>
201
202
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
232<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>
242
243
244<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
252<h2>$this->table->clear()</h2>
253
254<p>Lets you clear the table heading and row data. If you need to show multiple tables with different data you should
255to call this function after each table has been generated to empty the previous table information. Example:</p>
256
257<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>
276
277</div>
278<!-- END CONTENT -->
279
280
281<div id="footer">
282<p>
283Previous Topic:&nbsp;<a href="ftp.html">&nbsp;FTP Class</a>&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;
286Next Topic:&nbsp;&nbsp;<a href="image_lib.html">Image Manipulation Class</a>
287</p>
Derek Jones07870432008-02-13 03:49:26 +0000288<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
Derek Allard39b622d2008-01-16 21:10:09 +0000289</div>
290
291</body>
292</html>