Created
May 20, 2011 13:14
-
-
Save codersatx/982862 to your computer and use it in GitHub Desktop.
Table class used in Kohana to build tables programmatically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') OR die('No direct access allowed.'); | |
/** | |
* Table | |
* | |
* Handles the creation of tables programmatically | |
*/ | |
class Table{ | |
public $table_open; | |
public $table_rows; | |
public $table_close; | |
/** | |
* Construct | |
* | |
* Sets up the tables open and close tags | |
* | |
* @param string $id :used as id of the table in the html | |
* @param string $class :used as the css class of the table in html | |
* @return table | |
*/ | |
public function __construct($id,$class) | |
{ | |
$this->table_open = '<table cellpadding="0" cellspacing="0" id="'. $id .'" class="'. $class .'">'; | |
$this->table_close = '</table>'; | |
} | |
//------------------------------------------------------------------------------------------------------ | |
/** | |
* Rows | |
* | |
* Adds a new row to our table. Takes an array as the values for each cell. | |
* | |
* @param array $cells :array('cell1','cell2','cell3'') | |
* @param string $type :header or empty, so we can define a header row | |
* @return void :adds the newly created row to our public table_rows property | |
*/ | |
public function row($cells,$type='',$colspan=0,$class='regular_row') | |
{ | |
if($type=='header') | |
{ | |
$row = '<thead><tr>'; | |
} | |
else | |
{ | |
$row = '<tr>'; | |
} | |
foreach($cells as $cell) | |
{ | |
if($type=='header') | |
{ | |
if ($colspan != 0) | |
{ | |
$row .= '<th colspan="'.$colspan.'">'. $cell .'</th>'; | |
} | |
else | |
{ | |
$row .= '<th>'. $cell .'</th>'; | |
} | |
} | |
else | |
{ | |
if ($colspan != 0) | |
{ | |
$row .= '<td colspan="'.$colspan.'" class='. $class .'>'. $cell .'</td>'; | |
} | |
else | |
{ | |
$row .= '<td class='. $class .'>'. $cell .'</td>'; | |
} | |
} | |
} | |
if($type=='header') | |
{ | |
$row .= '</tr></thead>'; | |
} | |
else | |
{ | |
$row .= '</tr>'; | |
} | |
$this->table_rows .= $row; | |
} | |
//----------------------------------------------------------------------------------------------------- | |
/** | |
* Builds | |
* | |
* Puts all our pieces together and returns the output | |
* | |
* @return string $output :the fully built table ready to be passed to a view or controller | |
*/ | |
public function build() | |
{ | |
$output = $this->table_open; | |
$output .= $this->table_rows; | |
$output .= $this->table_close; | |
return $output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment