Public Member Functions | |
| CI_Table () | |
| set_template ($template) | |
| Set the template. | |
| set_heading () | |
| Set the table heading. | |
| make_columns ($array=array(), $col_limit=0) | |
| Set columns. | |
| set_empty ($value) | |
| Set "empty" cells. | |
| add_row () | |
| Add a table row. | |
| set_caption ($caption) | |
| Add a table caption. | |
| generate ($table_data=NULL) | |
| Generate the table. | |
| clear () | |
| Clears the table arrays. | |
| _set_from_object ($query) | |
| Set table data from a database result object. | |
| _set_from_array ($data, $set_heading=TRUE) | |
| Set table data from an array. | |
| _compile_template () | |
| Compile Template. | |
| _default_template () | |
| Default Template. | |
Public Attributes | |
| $rows = array() | |
| $heading = array() | |
| $auto_heading = TRUE | |
| $caption = NULL | |
| $template = NULL | |
| $newline = "\n" | |
| $empty_cells = "" | |
Definition at line 29 of file Table.php.
| CI_Table::_compile_template | ( | ) |
Compile Template.
private
Definition at line 385 of file Table.php.
References _default_template().
Referenced by generate().
00386 { 00387 if ($this->template == NULL) 00388 { 00389 $this->template = $this->_default_template(); 00390 return; 00391 } 00392 00393 $this->temp = $this->_default_template(); 00394 foreach (array('table_open','heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close') as $val) 00395 { 00396 if ( ! isset($this->template[$val])) 00397 { 00398 $this->template[$val] = $this->temp[$val]; 00399 } 00400 } 00401 }


| CI_Table::_default_template | ( | ) |
Default Template.
private
Definition at line 411 of file Table.php.
Referenced by _compile_template().
00412 { 00413 return array ( 00414 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', 00415 00416 'heading_row_start' => '<tr>', 00417 'heading_row_end' => '</tr>', 00418 'heading_cell_start' => '<th>', 00419 'heading_cell_end' => '</th>', 00420 00421 'row_start' => '<tr>', 00422 'row_end' => '</tr>', 00423 'cell_start' => '<td>', 00424 'cell_end' => '</td>', 00425 00426 'row_alt_start' => '<tr>', 00427 'row_alt_end' => '</tr>', 00428 'cell_alt_start' => '<td>', 00429 'cell_alt_end' => '</td>', 00430 00431 'table_close' => '</table>' 00432 ); 00433 }

| CI_Table::_set_from_array | ( | $ | data, | |
| $ | set_heading = TRUE | |||
| ) |
Set table data from an array.
public
| array |
Definition at line 347 of file Table.php.
Referenced by generate().
00348 { 00349 if ( ! is_array($data) OR count($data) == 0) 00350 { 00351 return FALSE; 00352 } 00353 00354 $i = 0; 00355 foreach ($data as $row) 00356 { 00357 if ( ! is_array($row)) 00358 { 00359 $this->rows[] = $data; 00360 break; 00361 } 00362 00363 // If a heading hasn't already been set we'll use the first row of the array as the heading 00364 if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE) 00365 { 00366 $this->heading = $row; 00367 } 00368 else 00369 { 00370 $this->rows[] = $row; 00371 } 00372 00373 $i++; 00374 } 00375 }

| CI_Table::_set_from_object | ( | $ | query | ) |
Set table data from a database result object.
public
| object |
Definition at line 309 of file Table.php.
Referenced by generate().
00310 { 00311 if ( ! is_object($query)) 00312 { 00313 return FALSE; 00314 } 00315 00316 // First generate the headings from the table column names 00317 if (count($this->heading) == 0) 00318 { 00319 if ( ! method_exists($query, 'list_fields')) 00320 { 00321 return FALSE; 00322 } 00323 00324 $this->heading = $query->list_fields(); 00325 } 00326 00327 // Next blast through the result array and build out the rows 00328 00329 if ($query->num_rows() > 0) 00330 { 00331 foreach ($query->result_array() as $row) 00332 { 00333 $this->rows[] = $row; 00334 } 00335 } 00336 }

| CI_Table::add_row | ( | ) |
| CI_Table::CI_Table | ( | ) |
Definition at line 40 of file Table.php.
References log_message().
00041 { 00042 log_message('debug', "Table Class Initialized"); 00043 }

| CI_Table::clear | ( | ) |
| CI_Table::generate | ( | $ | table_data = NULL |
) |
Generate the table.
public
| mixed |
Definition at line 185 of file Table.php.
References $heading, _compile_template(), _set_from_array(), and _set_from_object().
00186 { 00187 // The table data can optionally be passed to this function 00188 // either as a database result object or an array 00189 if ( ! is_null($table_data)) 00190 { 00191 if (is_object($table_data)) 00192 { 00193 $this->_set_from_object($table_data); 00194 } 00195 elseif (is_array($table_data)) 00196 { 00197 $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE; 00198 $this->_set_from_array($table_data, $set_heading); 00199 } 00200 } 00201 00202 // Is there anything to display? No? Smite them! 00203 if (count($this->heading) == 0 AND count($this->rows) == 0) 00204 { 00205 return 'Undefined table data'; 00206 } 00207 00208 // Compile and validate the template date 00209 $this->_compile_template(); 00210 00211 00212 // Build the table! 00213 00214 $out = $this->template['table_open']; 00215 $out .= $this->newline; 00216 00217 // Add any caption here 00218 if ($this->caption) 00219 { 00220 $out .= $this->newline; 00221 $out .= '<caption>' . $this->caption . '</caption>'; 00222 $out .= $this->newline; 00223 } 00224 00225 // Is there a table heading to display? 00226 if (count($this->heading) > 0) 00227 { 00228 $out .= $this->template['heading_row_start']; 00229 $out .= $this->newline; 00230 00231 foreach($this->heading as $heading) 00232 { 00233 $out .= $this->template['heading_cell_start']; 00234 $out .= $heading; 00235 $out .= $this->template['heading_cell_end']; 00236 } 00237 00238 $out .= $this->template['heading_row_end']; 00239 $out .= $this->newline; 00240 } 00241 00242 // Build the table rows 00243 if (count($this->rows) > 0) 00244 { 00245 $i = 1; 00246 foreach($this->rows as $row) 00247 { 00248 if ( ! is_array($row)) 00249 { 00250 break; 00251 } 00252 00253 // We use modulus to alternate the row colors 00254 $name = (fmod($i++, 2)) ? '' : 'alt_'; 00255 00256 $out .= $this->template['row_'.$name.'start']; 00257 $out .= $this->newline; 00258 00259 foreach($row as $cell) 00260 { 00261 $out .= $this->template['cell_'.$name.'start']; 00262 00263 if ($cell === "") 00264 { 00265 $out .= $this->empty_cells; 00266 } 00267 else 00268 { 00269 $out .= $cell; 00270 } 00271 00272 $out .= $this->template['cell_'.$name.'end']; 00273 } 00274 00275 $out .= $this->template['row_'.$name.'end']; 00276 $out .= $this->newline; 00277 } 00278 } 00279 00280 $out .= $this->template['table_close']; 00281 00282 return $out; 00283 }

| CI_Table::make_columns | ( | $ | array = array(), |
|
| $ | col_limit = 0 | |||
| ) |
Set columns.
Takes a one-dimensional array as input and creates a multi-dimensional array with a depth equal to the number of columns. This allows a single array with many elements to be displayed in a table that has a fixed column count.
public
| array | ||
| int |
Definition at line 94 of file Table.php.
00095 { 00096 if ( ! is_array($array) OR count($array) == 0) 00097 { 00098 return FALSE; 00099 } 00100 00101 // Turn off the auto-heading feature since it's doubtful we 00102 // will want headings from a one-dimensional array 00103 $this->auto_heading = FALSE; 00104 00105 if ($col_limit == 0) 00106 { 00107 return $array; 00108 } 00109 00110 $new = array(); 00111 while(count($array) > 0) 00112 { 00113 $temp = array_splice($array, 0, $col_limit); 00114 00115 if (count($temp) < $col_limit) 00116 { 00117 for ($i = count($temp); $i < $col_limit; $i++) 00118 { 00119 $temp[] = ' '; 00120 } 00121 } 00122 00123 $new[] = $temp; 00124 } 00125 00126 return $new; 00127 }
| CI_Table::set_caption | ( | $ | caption | ) |
| CI_Table::set_empty | ( | $ | value | ) |
| CI_Table::set_heading | ( | ) |
| CI_Table::set_template | ( | $ | template | ) |
| CI_Table::$caption = NULL |
| CI_Table::$heading = array() |
| CI_Table::$template = NULL |