Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################ |
| 2 | HTML Table Class |
| 3 | ################ |
| 4 | |
| 5 | The Table Class provides functions that enable you to auto-generate HTML |
| 6 | tables from arrays or database result sets. |
| 7 | |
| 8 | Initializing the Class |
| 9 | ====================== |
| 10 | |
| 11 | Like most other classes in CodeIgniter, the Table class is initialized |
| 12 | in your controller using the $this->load->library function:: |
| 13 | |
| 14 | $this->load->library('table'); |
| 15 | |
| 16 | Once loaded, the Table library object will be available using: |
| 17 | $this->table |
| 18 | |
| 19 | Examples |
| 20 | ======== |
| 21 | |
| 22 | Here is an example showing how you can create a table from a |
| 23 | multi-dimensional array. Note that the first array index will become the |
| 24 | table heading (or you can set your own headings using the set_heading() |
| 25 | function described in the function reference below). |
| 26 | |
| 27 | :: |
| 28 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 29 | $this->load->library('table'); |
| 30 | |
| 31 | $data = array( |
| 32 | array('Name', 'Color', 'Size'), |
| 33 | array('Fred', 'Blue', 'Small'), |
| 34 | array('Mary', 'Red', 'Large'), |
| 35 | array('John', 'Green', 'Medium') |
| 36 | ); |
| 37 | |
| 38 | echo $this->table->generate($data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 39 | |
| 40 | Here is an example of a table created from a database query result. The |
| 41 | table class will automatically generate the headings based on the table |
| 42 | names (or you can set your own headings using the set_heading() |
| 43 | function described in the function reference below). |
| 44 | |
| 45 | :: |
| 46 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 47 | $this->load->library('table'); |
| 48 | |
| 49 | $query = $this->db->query("SELECT * FROM my_table"); |
| 50 | |
| 51 | echo $this->table->generate($query); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
| 53 | Here is an example showing how you might create a table using discrete |
| 54 | parameters:: |
| 55 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 56 | $this->load->library('table'); |
| 57 | |
| 58 | $this->table->set_heading('Name', 'Color', 'Size'); |
| 59 | |
| 60 | $this->table->add_row('Fred', 'Blue', 'Small'); |
| 61 | $this->table->add_row('Mary', 'Red', 'Large'); |
| 62 | $this->table->add_row('John', 'Green', 'Medium'); |
| 63 | |
| 64 | echo $this->table->generate(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | |
| 66 | Here is the same example, except instead of individual parameters, |
| 67 | arrays are used:: |
| 68 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 69 | $this->load->library('table'); |
| 70 | |
| 71 | $this->table->set_heading(array('Name', 'Color', 'Size')); |
| 72 | |
| 73 | $this->table->add_row(array('Fred', 'Blue', 'Small')); |
| 74 | $this->table->add_row(array('Mary', 'Red', 'Large')); |
| 75 | $this->table->add_row(array('John', 'Green', 'Medium')); |
| 76 | |
| 77 | echo $this->table->generate(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
| 79 | Changing the Look of Your Table |
| 80 | =============================== |
| 81 | |
| 82 | The Table Class permits you to set a table template with which you can |
| 83 | specify the design of your layout. Here is the template prototype:: |
| 84 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 85 | $tmpl = array ( |
| 86 | 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', |
| 87 | |
| 88 | 'heading_row_start' => '<tr>', |
| 89 | 'heading_row_end' => '</tr>', |
| 90 | 'heading_cell_start' => '<th>', |
| 91 | 'heading_cell_end' => '</th>', |
| 92 | |
| 93 | 'row_start' => '<tr>', |
| 94 | 'row_end' => '</tr>', |
| 95 | 'cell_start' => '<td>', |
| 96 | 'cell_end' => '</td>', |
| 97 | |
| 98 | 'row_alt_start' => '<tr>', |
| 99 | 'row_alt_end' => '</tr>', |
| 100 | 'cell_alt_start' => '<td>', |
| 101 | 'cell_alt_end' => '</td>', |
| 102 | |
| 103 | 'table_close' => '</table>' |
| 104 | ); |
| 105 | |
| 106 | $this->table->set_template($tmpl); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
| 108 | .. note:: You'll notice there are two sets of "row" blocks in the |
| 109 | template. These permit you to create alternating row colors or design |
| 110 | elements that alternate with each iteration of the row data. |
| 111 | |
| 112 | You are NOT required to submit a complete template. If you only need to |
| 113 | change parts of the layout you can simply submit those elements. In this |
| 114 | example, only the table opening tag is being changed:: |
| 115 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 116 | $tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' ); |
| 117 | |
| 118 | $this->table->set_template($tmpl); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
| 120 | ****************** |
| 121 | Function Reference |
| 122 | ****************** |
| 123 | |
| 124 | $this->table->generate() |
| 125 | ======================== |
| 126 | |
| 127 | Returns a string containing the generated table. Accepts an optional |
| 128 | parameter which can be an array or a database result object. |
| 129 | |
| 130 | $this->table->set_caption() |
| 131 | ============================ |
| 132 | |
| 133 | Permits you to add a caption to the table. |
| 134 | |
| 135 | :: |
| 136 | |
| 137 | $this->table->set_caption('Colors'); |
| 138 | |
| 139 | $this->table->set_heading() |
| 140 | ============================ |
| 141 | |
| 142 | Permits you to set the table heading. You can submit an array or |
| 143 | discrete params:: |
| 144 | |
| 145 | $this->table->set_heading('Name', 'Color', 'Size'); |
| 146 | |
| 147 | :: |
| 148 | |
| 149 | $this->table->set_heading(array('Name', 'Color', 'Size')); |
| 150 | |
| 151 | $this->table->add_row() |
| 152 | ======================== |
| 153 | |
| 154 | Permits you to add a row to your table. You can submit an array or |
| 155 | discrete params:: |
| 156 | |
| 157 | $this->table->add_row('Blue', 'Red', 'Green'); |
| 158 | |
| 159 | :: |
| 160 | |
| 161 | $this->table->add_row(array('Blue', 'Red', 'Green')); |
| 162 | |
| 163 | If you would like to set an individual cell's tag attributes, you can |
| 164 | use an associative array for that cell. The associative key 'data' |
| 165 | defines the cell's data. Any other key => val pairs are added as |
| 166 | key='val' attributes to the tag:: |
| 167 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 168 | $cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2); |
| 169 | $this->table->add_row($cell, 'Red', 'Green'); |
| 170 | |
| 171 | // generates |
| 172 | // <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 173 | |
| 174 | $this->table->make_columns() |
| 175 | ============================= |
| 176 | |
| 177 | This function takes a one-dimensional array as input and creates a |
| 178 | multi-dimensional array with a depth equal to the number of columns |
| 179 | desired. This allows a single array with many elements to be displayed |
| 180 | in a table that has a fixed column count. Consider this example:: |
| 181 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 182 | $list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'); |
| 183 | |
| 184 | $new_list = $this->table->make_columns($list, 3); |
| 185 | |
| 186 | $this->table->generate($new_list); |
| 187 | |
| 188 | // Generates a table with this prototype |
| 189 | |
| 190 | <table border="0" cellpadding="4" cellspacing="0"> |
| 191 | <tr> |
| 192 | <td>one</td><td>two</td><td>three</td> |
| 193 | </tr><tr> |
| 194 | <td>four</td><td>five</td><td>six</td> |
| 195 | </tr><tr> |
| 196 | <td>seven</td><td>eight</td><td>nine</td> |
| 197 | </tr><tr> |
| 198 | <td>ten</td><td>eleven</td><td>twelve</td></tr> |
| 199 | </table> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 200 | |
| 201 | $this->table->set_template() |
| 202 | ============================= |
| 203 | |
| 204 | Permits you to set your template. You can submit a full or partial |
| 205 | template. |
| 206 | |
| 207 | :: |
| 208 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 209 | $tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' ); |
| 210 | |
| 211 | $this->table->set_template($tmpl); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 212 | |
| 213 | $this->table->set_empty() |
| 214 | ========================== |
| 215 | |
| 216 | Let's you set a default value for use in any table cells that are empty. |
| 217 | You might, for example, set a non-breaking space:: |
| 218 | |
| 219 | $this->table->set_empty(" "); |
| 220 | |
| 221 | $this->table->clear() |
| 222 | ===================== |
| 223 | |
| 224 | Lets you clear the table heading and row data. If you need to show |
| 225 | multiple tables with different data you should to call this function |
| 226 | after each table has been generated to empty the previous table |
| 227 | information. Example:: |
| 228 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 229 | $this->load->library('table'); |
| 230 | |
| 231 | $this->table->set_heading('Name', 'Color', 'Size'); |
| 232 | $this->table->add_row('Fred', 'Blue', 'Small'); |
| 233 | $this->table->add_row('Mary', 'Red', 'Large'); |
| 234 | $this->table->add_row('John', 'Green', 'Medium'); |
| 235 | |
| 236 | echo $this->table->generate(); |
| 237 | |
| 238 | $this->table->clear(); |
| 239 | |
| 240 | $this->table->set_heading('Name', 'Day', 'Delivery'); |
| 241 | $this->table->add_row('Fred', 'Wednesday', 'Express'); |
| 242 | $this->table->add_row('Mary', 'Monday', 'Air'); |
| 243 | $this->table->add_row('John', 'Saturday', 'Overnight'); |
| 244 | |
| 245 | echo $this->table->generate(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 246 | |
| 247 | $this->table->function |
| 248 | ====================== |
| 249 | |
| 250 | Allows you to specify a native PHP function or a valid function array |
| 251 | object to be applied to all cell data. |
| 252 | |
| 253 | :: |
| 254 | |
Derek Jones | d467862 | 2011-10-05 15:40:24 -0500 | [diff] [blame] | 255 | $this->load->library('table'); |
| 256 | |
| 257 | $this->table->set_heading('Name', 'Color', 'Size'); |
| 258 | $this->table->add_row('Fred', '<strong>Blue</strong>', 'Small'); |
| 259 | |
| 260 | $this->table->function = 'htmlspecialchars'; |
| 261 | echo $this->table->generate(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 262 | |
| 263 | In the above example, all cell data would be ran through PHP's |
| 264 | htmlspecialchars() function, resulting in:: |
| 265 | |
| 266 | <td>Fred</td><td><strong>Blue</strong></td><td>Small</td> |
| 267 | |