blob: e80e53fd75468ca93749b08d237a4185d2916f0a [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() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 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>
36<td><h1>Code Igniter User Guide Version 1.5.0b1</h1></td>
37<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</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
78<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.</p>
80
81<code>
82$this->load->library('table');<br />
83<br />
84$data = array(<br />
85&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Name', 'Color', 'Size'),<br />
86&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Fred', 'Blue', 'Small'),<br />
87&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Mary', 'Red', 'Large'),<br />
88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('John', 'Green', 'Medium') <br />
89&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
90<br />
91echo $this->table->generate($data);
92</code>
93
94<p>Here is an example of a table created from a database query result. The table class will automatically generate the
95headings based on the table names. Or you can set your own headings using the <dfn>set_heading()</dfn> function described
96in the function reference below.</p>
97
98<code>
99$this->load->library('table');<br />
100<br />
101$query = $this->db->query("SELECT * FROM my_table");<br />
102<br />
103echo $this->table->generate($query);
104</code>
105
106
107<p>Here is an example showing how you might create a table using discreet parameters:</p>
108
109<code>
110$this->load->library('table');<br />
111<br />
112$this->table->set_heading('Name', 'Color', 'Size');<br />
113<br />
114$this->table->add_row('Fred', 'Blue', 'Small');<br />
115$this->table->add_row('Mary', 'Red', 'Large');<br />
116$this->table->add_row('John', 'Green', 'Medium');<br />
117<br />
118echo $this->table->generate();
119</code>
120
121<p>Here is the same example, except instead of individual parameters, arrays are used:
122
123<code>
124$this->load->library('table');<br />
125<br />
126$this->table->set_heading(array('Name', 'Color', 'Size'));<br />
127<br />
128$this->table->add_row(array('Fred', 'Blue', 'Small'));<br />
129$this->table->add_row(array('Mary', 'Red', 'Large'));<br />
130$this->table->add_row(array('John', 'Green', 'Medium'));<br />
131<br />
132echo $this->table->generate();
133</code>
134
135
136<h2>Changing the Look of Your Table</h2>
137
138<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
139prototype:</p>
140
141<code>
142$tmpl = array (<br />
143&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 />
144<br />
145&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 />
146&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 />
147&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 />
148&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 />
149<br />
150&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'row_start'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '&lt;tr>',<br />
151&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n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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 />
158&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 />
159<br />
160&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 />
161&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
162
163<br />
164$this->table->set_template($tmpl);
165</code>
166
167<p class="important">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
168iteration of the row data.</p>
169
170<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.
171In this example, only the table opening tag is being changed:</p>
172
173<code>
174$tmpl = array ( 'table_open'&nbsp;&nbsp;=> '&lt;table border="1" cellpadding="2" cellspacing="1" class="mytable">' );<br />
175
176<br />
177$this->table->set_template($tmpl);
178</code>
179
180<br />
181<h1>Function Reference</h1>
182
183<h2>$this->table->generate()</h2>
184<p>Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.</p>
185
186<h2>$this->table->set_heading()</h2>
187
188<p>Permits you to set the table heading. You can submit an array or discreet params:</p>
189
190<code>$this->table->set_heading('Name', 'Color', 'Size');</code>
191<code>$this->table->set_heading(array('Name', 'Color', 'Size'));</code>
192
193<h2>$this->table->add_row()</h2>
194
195<p>Permits you to add a row to your table. You can submit an array or discreet params:</p>
196
197<code>$this->table->add_row('Blue', 'Red', 'Green');</code>
198<code>$this->table->add_row(array('Blue', 'Red', 'Green'));</code>
199
200<h2>$this->table->set_template()</h2>
201
202<p>Permits you to set your template. You can submit a full or partial template.</p>
203
204<code>
205$tmpl = array ( 'table_open'&nbsp;&nbsp;=> '&lt;table border="1" cellpadding="2" cellspacing="1" class="mytable">' );<br />
206
207<br />
208$this->table->set_template($tmpl);
209</code>
admin9fd91c02006-10-09 03:30:36 +0000210
211
212</div>
213<!-- END CONTENT -->
214
215
216<div id="footer">
217<p>
218Previous Topic:&nbsp;&nbsp;<a href="sessions.html">Session Class</a>
219&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
220<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
221<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
222Next Topic:&nbsp;&nbsp;<a href="trackback.html">Trackback Class</a>
223<p>
224<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>
225</div>
226
227</body>
228</html>