diff options
author | admin <devnull@localhost> | 2006-10-28 07:22:02 +0200 |
---|---|---|
committer | admin <devnull@localhost> | 2006-10-28 07:22:02 +0200 |
commit | 26364b934dc8f7fa25f26e1f172d2a13dd826a15 (patch) | |
tree | 89e83c465e1123de07063266d50fdea246688564 | |
parent | c7e5999d0b3d5a0742acd5aa57f43bdbae6da6fa (diff) |
-rw-r--r-- | system/libraries/Table.php | 79 | ||||
-rw-r--r-- | user_guide/libraries/table.html | 30 |
2 files changed, 95 insertions, 14 deletions
diff --git a/system/libraries/Table.php b/system/libraries/Table.php index da7bbbecf..01c45c73b 100644 --- a/system/libraries/Table.php +++ b/system/libraries/Table.php @@ -28,11 +28,12 @@ */ class CI_Table { - var $rows = array(); - var $heading = array(); - var $template = NULL; - var $newline = "\n"; - var $empty_cells = ""; + var $rows = array(); + var $heading = array(); + var $auto_heading = TRUE; + var $template = NULL; + var $newline = "\n"; + var $empty_cells = ""; function CI_Table() @@ -79,6 +80,55 @@ class CI_Table { // -------------------------------------------------------------------- /** + * 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. + * + * @access public + * @param array + * @param int + * @return void + */ + function make_columns($array = array(), $col_limit = 0) + { + if ( ! is_array($array) OR count($array) == 0) + { + return FALSE; + } + + // Turn off the auto-heading feature since it's doubtful we + // will want headings from a one-dimensional array + $this->auto_heading = FALSE; + + if ($col_limit == 0) + { + return $array; + } + + $new = array(); + while(count($array) > 0) + { + $temp = array_slice($array, 0, $col_limit); + $array = array_diff($array, $temp); + + if (count($temp) < $col_limit) + { + for ($i = count($temp); $i < $col_limit; $i++) + { + $temp[] = ' '; + } + } + + $new[] = $temp; + } + + return $new; + } + + // -------------------------------------------------------------------- + + /** * Set "empty" cells * * Can be passed as an array or discreet params @@ -130,7 +180,8 @@ class CI_Table { } elseif (is_array($table_data)) { - $this->_set_from_array($table_data); + $set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE; + $this->_set_from_array($table_data, $set_heading); } } @@ -178,14 +229,14 @@ class CI_Table { } // We use modulus to alternate the row colors - $alt = (fmod($i++, 2)) ? '' : 'alt_'; + $name = (fmod($i++, 2)) ? '' : 'alt_'; - $out .= $this->template['row_'.$alt.'start']; + $out .= $this->template['row_'.$name.'start']; $out .= $this->newline; foreach($row as $cell) { - $out .= $this->template['cell_'.$alt.'start']; + $out .= $this->template['cell_'.$name.'start']; if ($cell == "") { @@ -196,10 +247,10 @@ class CI_Table { $out .= $cell; } - $out .= $this->template['cell_'.$alt.'end']; + $out .= $this->template['cell_'.$name.'end']; } - $out .= $this->template['row_'.$alt.'end']; + $out .= $this->template['row_'.$name.'end']; $out .= $this->newline; } } @@ -208,7 +259,7 @@ class CI_Table { return $out; } - + // -------------------------------------------------------------------- /** @@ -256,7 +307,7 @@ class CI_Table { * @param array * @return void */ - function _set_from_array($data) + function _set_from_array($data, $set_heading = TRUE) { if ( ! is_array($data) OR count($data) == 0) { @@ -273,7 +324,7 @@ class CI_Table { } // If a heading hasn't already been set we'll use the first row of the array as the heading - if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0) + if ($i == 0 AND count($data) > 1 AND count($this->heading) == 0 AND $set_heading == TRUE) { $this->heading = $row; } diff --git a/user_guide/libraries/table.html b/user_guide/libraries/table.html index abe1a2bf0..880017acf 100644 --- a/user_guide/libraries/table.html +++ b/user_guide/libraries/table.html @@ -198,6 +198,36 @@ $this->table->set_template($tmpl); <code>$this->table->add_row('Blue', 'Red', 'Green');</code>
<code>$this->table->add_row(array('Blue', 'Red', 'Green'));</code>
+
+<h2>$this->table->make_columns()</h2>
+
+<p>This function takes a one-dimensional array as input and creates
+a multi-dimensional array with a depth equal to the number of
+columns desired. This allows a single array with many elements to be
+displayed in a table that has a fixed column count. Consider this example:</p>
+
+<code>
+$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');<br />
+<br />
+$new_list = $this->table->make_columns($list, 3);<br />
+<br />
+$this->table->generate($new_list)<br />
+<br />
+// Generates a table with this prototype<br />
+<br />
+<table border="0" cellpadding="4" cellspacing="0"><br />
+<tr><br />
+<td>one</td><td>two</td><td>three</td><br />
+</tr><tr><br />
+<td>four</td><td>five</td><td>six</td><br />
+</tr><tr><br />
+<td>seven</td><td>eight</td><td>nine</td><br />
+</tr><tr><br />
+<td>ten</td><td>eleven</td><td>twelve</td></tr><br />
+</table></code>
+
+
+
<h2>$this->table->set_template()</h2>
<p>Permits you to set your template. You can submit a full or partial template.</p>
|