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