summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoradmin <devnull@localhost>2006-10-28 07:22:02 +0200
committeradmin <devnull@localhost>2006-10-28 07:22:02 +0200
commit26364b934dc8f7fa25f26e1f172d2a13dd826a15 (patch)
tree89e83c465e1123de07063266d50fdea246688564
parentc7e5999d0b3d5a0742acd5aa57f43bdbae6da6fa (diff)
-rw-r--r--system/libraries/Table.php79
-rw-r--r--user_guide/libraries/table.html30
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[] = '&nbsp;';
+ }
+ }
+
+ $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 />
+&lt;table border="0" cellpadding="4" cellspacing="0"&gt;<br />
+&lt;tr&gt;<br />
+&lt;td&gt;one&lt;/td&gt;&lt;td&gt;two&lt;/td&gt;&lt;td&gt;three&lt;/td&gt;<br />
+&lt;/tr&gt;&lt;tr&gt;<br />
+&lt;td&gt;four&lt;/td&gt;&lt;td&gt;five&lt;/td&gt;&lt;td&gt;six&lt;/td&gt;<br />
+&lt;/tr&gt;&lt;tr&gt;<br />
+&lt;td&gt;seven&lt;/td&gt;&lt;td&gt;eight&lt;/td&gt;&lt;td&gt;nine&lt;/td&gt;<br />
+&lt;/tr&gt;&lt;tr&gt;<br />
+&lt;td&gt;ten&lt;/td&gt;&lt;td&gt;eleven&lt;/td&gt;&lt;td&gt;twelve&lt;/td&gt;&lt;/tr&gt;<br />
+&lt;/table&gt;</code>
+
+
+
<h2>$this->table->set_template()</h2>
<p>Permits you to set your template. You can submit a full or partial template.</p>