1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.3.1
16 // ------------------------------------------------------------------------
19 * HTML Table Generating Class
21 * Lets you create tables manually or from database result objects, or arrays.
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category HTML Tables
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/uri.html
32 var $heading = array();
33 var $auto_heading = TRUE;
37 var $empty_cells = "";
42 log_message('debug', "Table Class Initialized");
45 // --------------------------------------------------------------------
54 function set_template($template)
56 if ( ! is_array($template))
61 $this->template = $template;
64 // --------------------------------------------------------------------
67 * Set the table heading
69 * Can be passed as an array or discreet params
75 function set_heading()
77 $args = func_get_args();
78 $this->heading = (is_array($args[0])) ? $args[0] : $args;
81 // --------------------------------------------------------------------
84 * Set columns. Takes a one-dimensional array as input and creates
85 * a multi-dimensional array with a depth equal to the number of
86 * columns. This allows a single array with many elements to be
87 * displayed in a table that has a fixed column count.
94 function make_columns($array = array(), $col_limit = 0)
96 if ( ! is_array($array) OR count($array) == 0)
101 // Turn off the auto-heading feature since it's doubtful we
102 // will want headings from a one-dimensional array
103 $this->auto_heading = FALSE;
111 while(count($array) > 0)
113 $temp = array_splice($array, 0, $col_limit);
115 if (count($temp) < $col_limit)
117 for ($i = count($temp); $i < $col_limit; $i++)
129 // --------------------------------------------------------------------
134 * Can be passed as an array or discreet params
140 function set_empty($value)
142 $this->empty_cells = $value;
145 // --------------------------------------------------------------------
150 * Can be passed as an array or discreet params
158 $args = func_get_args();
159 $this->rows[] = (is_array($args[0])) ? $args[0] : $args;
162 // --------------------------------------------------------------------
165 * Add a table caption
171 function set_caption($caption)
173 $this->caption = $caption;
176 // --------------------------------------------------------------------
185 function generate($table_data = NULL)
187 // The table data can optionally be passed to this function
188 // either as a database result object or an array
189 if ( ! is_null($table_data))
191 if (is_object($table_data))
193 $this->_set_from_object($table_data);
195 elseif (is_array($table_data))
197 $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
198 $this->_set_from_array($table_data, $set_heading);
202 // Is there anything to display? No? Smite them!
203 if (count($this->heading) == 0 AND count($this->rows) == 0)
205 return 'Undefined table data';
208 // Compile and validate the template date
209 $this->_compile_template();
214 $out = $this->template['table_open'];
215 $out .= $this->newline;
217 // Add any caption here
220 $out .= $this->newline;
221 $out .= '<caption>' . $this->caption . '</caption>';
222 $out .= $this->newline;
225 // Is there a table heading to display?
226 if (count($this->heading) > 0)
228 $out .= $this->template['heading_row_start'];
229 $out .= $this->newline;
231 foreach($this->heading as $heading)
233 $out .= $this->template['heading_cell_start'];
235 $out .= $this->template['heading_cell_end'];
238 $out .= $this->template['heading_row_end'];
239 $out .= $this->newline;
242 // Build the table rows
243 if (count($this->rows) > 0)
246 foreach($this->rows as $row)
248 if ( ! is_array($row))
253 // We use modulus to alternate the row colors
254 $name = (fmod($i++, 2)) ? '' : 'alt_';
256 $out .= $this->template['row_'.$name.'start'];
257 $out .= $this->newline;
259 foreach($row as $cell)
261 $out .= $this->template['cell_'.$name.'start'];
265 $out .= $this->empty_cells;
272 $out .= $this->template['cell_'.$name.'end'];
275 $out .= $this->template['row_'.$name.'end'];
276 $out .= $this->newline;
280 $out .= $this->template['table_close'];
285 // --------------------------------------------------------------------
288 * Clears the table arrays. Useful if multiple tables are being generated
295 $this->rows = array();
296 $this->heading = array();
297 $this->auto_heading = TRUE;
300 // --------------------------------------------------------------------
303 * Set table data from a database result object
309 function _set_from_object($query)
311 if ( ! is_object($query))
316 // First generate the headings from the table column names
317 if (count($this->heading) == 0)
319 if ( ! method_exists($query, 'list_fields'))
324 $this->heading = $query->list_fields();
327 // Next blast through the result array and build out the rows
329 if ($query->num_rows() > 0)
331 foreach ($query->result_array() as $row)
333 $this->rows[] = $row;
338 // --------------------------------------------------------------------
341 * Set table data from an array
347 function _set_from_array($data, $set_heading = TRUE)
349 if ( ! is_array($data) OR count($data) == 0)
355 foreach ($data as $row)
357 if ( ! is_array($row))
359 $this->rows[] = $data;
363 // If a heading hasn't already been set we'll use the first row of the array as the heading
364 if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE)
366 $this->heading = $row;
370 $this->rows[] = $row;
377 // --------------------------------------------------------------------
385 function _compile_template()
387 if ($this->template == NULL)
389 $this->template = $this->_default_template();
393 $this->temp = $this->_default_template();
394 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)
396 if ( ! isset($this->template[$val]))
398 $this->template[$val] = $this->temp[$val];
403 // --------------------------------------------------------------------
411 function _default_template()
414 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">',
416 'heading_row_start' => '<tr>',
417 'heading_row_end' => '</tr>',
418 'heading_cell_start' => '<th>',
419 'heading_cell_end' => '</th>',
421 'row_start' => '<tr>',
422 'row_end' => '</tr>',
423 'cell_start' => '<td>',
424 'cell_end' => '</td>',
426 'row_alt_start' => '<tr>',
427 'row_alt_end' => '</tr>',
428 'cell_alt_start' => '<td>',
429 'cell_alt_end' => '</td>',
431 'table_close' => '</table>'
439 /* End of file Table.php */
440 /* Location: ./system/libraries/Table.php */