Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 3 | <head> |
| 4 | |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 6 | <title>HTML Table Class : CodeIgniter User Guide</title> |
| 7 | |
| 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 | |
| 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='ExpressionEngine Dev Team' /> |
| 20 | <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_darker.jpg" width="154" height="43" 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 Jones | bbedc76 | 2009-09-11 14:47:37 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.7.2</h1></td> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 32 | <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"> |
| 43 | <a href="http://codeigniter.com/">CodeIgniter Home</a> › |
| 44 | <a href="../index.html">User Guide Home</a> › |
| 45 | HTML 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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <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. |
| 74 | Note 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 | array('Name', 'Color', 'Size'),<br /> |
| 82 | array('Fred', 'Blue', 'Small'),<br /> |
| 83 | array('Mary', 'Red', 'Large'),<br /> |
| 84 | array('John', 'Green', 'Medium') <br /> |
| 85 | );<br /> |
| 86 | <br /> |
| 87 | echo $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 |
| 91 | headings based on the table names (or you can set your own headings using the <dfn>set_heading()</dfn> function described |
| 92 | in 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 /> |
| 99 | echo $this->table->generate($query); |
| 100 | </code> |
| 101 | |
| 102 | |
| 103 | <p>Here is an example showing how you might create a table using discrete 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 /> |
| 114 | echo $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 /> |
| 128 | echo $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 |
| 135 | prototype:</p> |
| 136 | |
| 137 | <code> |
| 138 | $tmpl = array (<br /> |
| 139 | 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',<br /> |
| 140 | <br /> |
| 141 | 'heading_row_start' => '<tr>',<br /> |
| 142 | 'heading_row_end' => '</tr>',<br /> |
| 143 | 'heading_cell_start' => '<th>',<br /> |
| 144 | 'heading_cell_end' => '</th>',<br /> |
| 145 | <br /> |
| 146 | 'row_start' => '<tr>',<br /> |
| 147 | 'row_end' => '</tr>',<br /> |
| 148 | 'cell_start' => '<td>',<br /> |
| 149 | 'cell_end' => '</td>',<br /> |
| 150 | <br /> |
| 151 | 'row_alt_start' => '<tr>',<br /> |
| 152 | 'row_alt_end' => '</tr>',<br /> |
| 153 | 'cell_alt_start' => '<td>',<br /> |
| 154 | 'cell_alt_end' => '</td>',<br /> |
| 155 | <br /> |
| 156 | 'table_close' => '</table>'<br /> |
| 157 | );<br /> |
| 158 | |
| 159 | <br /> |
| 160 | $this->table->set_template($tmpl); |
| 161 | </code> |
| 162 | |
| 163 | <p class="important"><strong>Note:</strong> 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 |
| 164 | iteration 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. |
| 167 | In this example, only the table opening tag is being changed:</p> |
| 168 | |
| 169 | <code> |
| 170 | $tmpl = array ( 'table_open' => '<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 | |
| 190 | <p>Permits you to set the table heading. You can submit an array or discrete 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 discrete 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 | |
| 202 | |
| 203 | <h2>$this->table->make_columns()</h2> |
| 204 | |
| 205 | <p>This function takes a one-dimensional array as input and creates |
| 206 | a multi-dimensional array with a depth equal to the number of |
| 207 | columns desired. This allows a single array with many elements to be |
| 208 | displayed 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 | <table border="0" cellpadding="4" cellspacing="0"><br /> |
| 220 | <tr><br /> |
| 221 | <td>one</td><td>two</td><td>three</td><br /> |
| 222 | </tr><tr><br /> |
| 223 | <td>four</td><td>five</td><td>six</td><br /> |
| 224 | </tr><tr><br /> |
| 225 | <td>seven</td><td>eight</td><td>nine</td><br /> |
| 226 | </tr><tr><br /> |
| 227 | <td>ten</td><td>eleven</td><td>twelve</td></tr><br /> |
| 228 | </table></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' => '<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("&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 |
| 255 | to 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 /> |
| 265 | echo $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 /> |
| 274 | echo $this->table->generate(); |
| 275 | </code> |
| 276 | |
| 277 | </div> |
| 278 | <!-- END CONTENT --> |
| 279 | |
| 280 | |
| 281 | <div id="footer"> |
| 282 | <p> |
| 283 | Previous Topic: <a href="ftp.html"> FTP Class</a> · |
| 284 | <a href="#top">Top of Page</a> · |
| 285 | <a href="../index.html">User Guide Home</a> · |
| 286 | Next Topic: <a href="image_lib.html">Image Manipulation Class</a> |
| 287 | </p> |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 288 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2010 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p> |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | </div> |
| 290 | |
| 291 | </body> |
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 292 | </html> |