2 /*------------------------------------------------------------
3 * CarbonPHP framework (C) Tom Bell
4 * http://tombell.org.uk
5 *------------------------------------------------------------*/
7 if (!defined('CARBON_PATH'))
9 exit('Direct script access is not allowed.');
14 private $rows = array();
15 private $heading = array();
16 private $auto_heading = true;
17 private $caption = null;
18 private $template = null;
19 private $newline = "\n";
20 private $empty_cells = '';
22 public function __construct()
24 log_message('debug', 'Table class initialiased.');
27 public function set_template($template)
29 if (!is_array($template))
34 $this->template
= $template;
39 public function set_heading()
41 $args = func_get_args();
42 $this->heading
= (is_array($args[0])) ?
$args[0] : $args;
45 public function make_columns($array = array(), $col_limit = 0)
47 if (!is_array($array) ||
count($array) == 0)
52 $this->auto_heading
= false;
61 while (count($array) > 0)
63 $temp = array_slice($array, 0, $col_limit);
64 $array = array_diff($array, $temp);
66 if (count($temp) < $col_limit)
68 for ($i = count($temp); $i < $col_limit; $i++
)
80 public function set_empty($value)
82 $this->empty_cells
= $value;
85 public function add_row()
87 $args = func_get_args();
88 $this->rows
[] = (is_array($args[0])) ?
$args[0] : $args;
91 public function set_caption($caption)
93 $this->caption
= $caption;
96 public function generate($table_data = null)
98 if (!is_null($table_data))
100 if (is_object($table_data))
102 $this->c_set_from_object($table_data);
104 elseif (is_array($table_data))
106 $set_heading = (count($this->heading
) == 0 && $this->auto_heading
== false) ?
false : true;
107 $this->c_set_from_array($table_data, $set_heading);
111 if (count($this->heading
) == 0 && count($this->rows
) == 0)
113 return 'Undefined table data';
116 $this->c_compile_template();
118 $out = $this->template
['table_open'];
119 $out .= $this->newline
;
123 $out .= $this->newline
;
124 $out .= '<caption>' . $this->caption
. '</caption>';
125 $out .= $this->newline
;
128 if (count($this->heading
) > 0)
130 $out .= $this->template
['heading_row_start'];
131 $out .= $this->newline
;
133 foreach ($this->heading
as $heading)
135 $out .= $this->template
['heading_cell_start'];
137 $out .= $this->template
['heading_cell_end'];
140 $out .= $this->template
['heading_row_end'];
141 $out .= $this->newline
;
144 if (count($this->rows
) > 0)
148 foreach ($this->rows
as $row)
155 $name = (fmod($i++
, 2)) ?
'' : 'alt_';
157 $out .= $this->template
['row_' . $name . 'start'];
158 $out .= $this->newline
;
160 foreach ($row as $cell)
162 $out .= $this->template
['cell_' . $name . 'start'];
166 $out .= $this->empty_cells
;
173 $out .= $this->template
['cell_' . $name . 'end'];
176 $out .= $this->template
['row_' . $name . 'end'];
177 $out .= $this->newline
;
181 $out .= $this->template
['table_close'];
186 public function clear()
188 $this->rows
= array();
189 $this->heading
= array();
190 $this->auto_heading
= true;
193 private function c_set_from_object($query)
195 if (!is_object($query))
200 if (count($this->heading
) == 0)
202 if (!method_exists($query, 'list_fields'))
207 $this->heading
= $query->list_fields();
210 if ($query->num_rows() > 0)
212 foreach ($query->result_array() as $row)
214 $this->rows
[] = $row;
219 private function c_set_from_array($data, $set_heading = true)
221 if (!is_array($data) ||
count($data) == 0)
228 foreach ($data as $row)
232 $this->rows
[] = $data;
236 if ($i == 0 && count($data) > 1 && count($this->heading
) == 0 && $set_heading == true)
238 $this->heading
= $row;
242 $this->rows
[] = $row;
249 private function c_compile_template()
251 if ($this->template
== null)
253 $this->template
= $this->c_default_template();
257 $this->temp
= $this->c_default_template();
262 'heading_cell_start',
275 foreach ($template as $value)
277 if (!isset($this->template
[$value]))
279 $this->template
[$value] = $this->temp
[$value];
284 private function c_default_template()
287 'table_open' => '<table border="0" cellingpadding="4" cellspacing="0">',
288 'heading_row_start' => '<tr>',
289 'heading_row_end' => '</tr>',
290 'heading_cell_start' => '<th>',
291 'heading_cell_end' => '</th>',
292 'row_start' => '<tr>',
293 'row_end' => '</tr>',
294 'cell_start' => '<td>',
295 'cell_end' => '</td>',
296 'row_alt_start' => '<tr>',
297 'row_alt_end' => '</tr>',
298 'cell_alt_start' => '<td>',
299 'cell_alt_end' => '</td>',
300 'table_close' => '</table>'