Skip to content

Instantly share code, notes, and snippets.

@codersatx
Created May 20, 2011 13:14
Show Gist options
  • Save codersatx/982862 to your computer and use it in GitHub Desktop.
Save codersatx/982862 to your computer and use it in GitHub Desktop.
Table class used in Kohana to build tables programmatically.
<?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