blob: c2e3239933d8ac423ac559d17d8e62cf679fa1ee [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
adminb1fddc02006-10-09 21:29:07 +000075<p>You can also set your own class variable name. Please see the <a href="loader.html">Loader Class</a> for more info.</p>
76
admin9fd91c02006-10-09 03:30:36 +000077
admina1931ad2006-10-09 04:17:38 +000078<h2>Examples</h2>
79
80<p>Here is an example showing how you can create a table from a multi-dimensional array.
admin2f0bac82006-10-09 17:36:45 +000081Note that the first array index will become the table heading (or you can set your own headings using the
82<dfn>set_heading()</dfn> function described in the function reference below).</p>
admina1931ad2006-10-09 04:17:38 +000083
84<code>
85$this->load->library('table');<br />
86<br />
87$data = array(<br />
88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Name', 'Color', 'Size'),<br />
89&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Fred', 'Blue', 'Small'),<br />
90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('Mary', 'Red', 'Large'),<br />
91&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('John', 'Green', 'Medium') <br />
92&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
93<br />
94echo $this->table->generate($data);
95</code>
96
97<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 +000098headings based on the table names (or you can set your own headings using the <dfn>set_heading()</dfn> function described
99in the function reference below).</p>
admina1931ad2006-10-09 04:17:38 +0000100
101<code>
102$this->load->library('table');<br />
103<br />
104$query = $this->db->query("SELECT * FROM my_table");<br />
105<br />
106echo $this->table->generate($query);
107</code>
108
109
110<p>Here is an example showing how you might create a table using discreet parameters:</p>
111
112<code>
113$this->load->library('table');<br />
114<br />
115$this->table->set_heading('Name', 'Color', 'Size');<br />
116<br />
117$this->table->add_row('Fred', 'Blue', 'Small');<br />
118$this->table->add_row('Mary', 'Red', 'Large');<br />
119$this->table->add_row('John', 'Green', 'Medium');<br />
120<br />
121echo $this->table->generate();
122</code>
123
124<p>Here is the same example, except instead of individual parameters, arrays are used:
125
126<code>
127$this->load->library('table');<br />
128<br />
129$this->table->set_heading(array('Name', 'Color', 'Size'));<br />
130<br />
131$this->table->add_row(array('Fred', 'Blue', 'Small'));<br />
132$this->table->add_row(array('Mary', 'Red', 'Large'));<br />
133$this->table->add_row(array('John', 'Green', 'Medium'));<br />
134<br />
135echo $this->table->generate();
136</code>
137
138
139<h2>Changing the Look of Your Table</h2>
140
141<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
142prototype:</p>
143
144<code>
145$tmpl = array (<br />
146&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 />
147<br />
148&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 />
149&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 />
150&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 />
151&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 />
152<br />
153&nbsp;&nbsp;&nbsp;&nbsp;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sp;&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 />
160&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 />
161&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 />
162<br />
163&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 />
164&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
165
166<br />
167$this->table->set_template($tmpl);
168</code>
169
admin2f0bac82006-10-09 17:36:45 +0000170<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 +0000171iteration of the row data.</p>
172
173<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.
174In this example, only the table opening tag is being changed:</p>
175
176<code>
177$tmpl = array ( 'table_open'&nbsp;&nbsp;=> '&lt;table border="1" cellpadding="2" cellspacing="1" class="mytable">' );<br />
178
179<br />
180$this->table->set_template($tmpl);
181</code>
182
183<br />
184<h1>Function Reference</h1>
185
186<h2>$this->table->generate()</h2>
187<p>Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.</p>
188
189<h2>$this->table->set_heading()</h2>
190
191<p>Permits you to set the table heading. You can submit an array or discreet params:</p>
192
193<code>$this->table->set_heading('Name', 'Color', 'Size');</code>
194<code>$this->table->set_heading(array('Name', 'Color', 'Size'));</code>
195
196<h2>$this->table->add_row()</h2>
197
198<p>Permits you to add a row to your table. You can submit an array or discreet params:</p>
199
200<code>$this->table->add_row('Blue', 'Red', 'Green');</code>
201<code>$this->table->add_row(array('Blue', 'Red', 'Green'));</code>
202
203<h2>$this->table->set_template()</h2>
204
205<p>Permits you to set your template. You can submit a full or partial template.</p>
206
207<code>
208$tmpl = array ( 'table_open'&nbsp;&nbsp;=> '&lt;table border="1" cellpadding="2" cellspacing="1" class="mytable">' );<br />
209
210<br />
211$this->table->set_template($tmpl);
212</code>
admin9fd91c02006-10-09 03:30:36 +0000213
214
215</div>
216<!-- END CONTENT -->
217
218
219<div id="footer">
220<p>
221Previous Topic:&nbsp;&nbsp;<a href="sessions.html">Session Class</a>
222&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
223<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
224<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
225Next Topic:&nbsp;&nbsp;<a href="trackback.html">Trackback Class</a>
226<p>
227<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>
228</div>
229
230</body>
231</html>