Skip to content

Instantly share code, notes, and snippets.

@blackfyre
Created April 29, 2014 19:05
Show Gist options
  • Save blackfyre/11409046 to your computer and use it in GitHub Desktop.
Save blackfyre/11409046 to your computer and use it in GitHub Desktop.
Bootstrap Table generation from PHP
<?php
class bootTable {
/**
* @param array $attributes
* @return null|string
*/
static function parseAttributes($attributes = array())
{
if (self::checkArray($attributes)) {
$boolean = array('disabled');
$excluded = array('iconLeft', 'iconRight', 'head', 'responsive', 'footer');
$string = null;
foreach ($attributes AS $k => $v) {
if (!in_array($k, $excluded)) {
if (in_array($k, $boolean)) {
$string .= ' ' . $k . ' ';
} else {
if (!is_null($v)) {
$string .= ' ' . $k . '="' . $v . '" ';
}
}
}
}
return $string;
}
return null;
}
/**
* @param string $attributeToAdd
* @param string $attributeValue
* @param array $attributes
* @param bool $overwrite
* @return array|bool
*/
private static function addAttribute($attributeToAdd = '', $attributeValue = '', $attributes = array(), $overwrite = true) {
if (self::checkArray($attributes)) {
$attributeKeys = array_keys($attributes);
if (in_array($attributeToAdd,$attributeKeys)) {
if ($overwrite) {
$attributes[$attributeToAdd] = $attributeValue;
} else {
$content = explode(' ',$attributes[$attributeToAdd]);
$content[] = $attributeValue;
$attributes[$attributeToAdd] = implode(' ',$content);
}
} else {
$attributes[$attributeToAdd] = $attributeValue;
}
return $attributes;
}
return false;
}
/**
* A function to generate bootstrap tables
*
* @param array $heads The table heads, also present in footer if required
* @param array $content
* @param array $attributes
* @return null|string
*/
public static function createTable($heads = array(), $content = array(), $attributes = array()) {
if (self::checkArray($heads) && self::checkArray($content)) {
/* Get all the available table heads defined in $heads */
$colsInTable = array_keys($heads);
/* fix in the table class as it's a requirement */
$attributes = self::addAttribute('class','table',$attributes,false);
/* Create a var to generate the table */
$r = null;
/* Add responsive container if required */
if (isset($attributes['responsive']) && $attributes['responsive'] == true)
$r .= '<div class="table-responsive">' . "\r\n";
/* Create the table */
$r .= '<table ' . self::parseAttributes($attributes) . '>';
/* Create table head */
$r .= '<thead>' . "\r\n";
$r .= '<tr>' . "\r\n";
foreach ($heads AS $head) {
if (self::checkArray($head)) {
$r .= '<th ' . self::parseAttributes($head['attr']) . '>';
$r .= $head['title'];
$r .= '</th>' . "\r\n";
} else {
$r .= '<th>';
$r .= $head;
$r .= '</th>' . "\r\n";
}
}
/* Close table head */
$r .= '</tr>' . "\r\n";
$r .= '</thead>' . "\r\n";
/* Create table body */
$r .= '<tbody>' . "\r\n";
foreach ($content AS $row) {
$r .= '<tr' . self::parseAttributes($row['attr']) .'>' . "\r\n";
foreach ($colsInTable AS $colName) {
$r .= '<td>';
$r .= (isset($row[$colName]) ? $row[$colName] : '&nbsp;');
$r .= '</td>' . "\r\n";
}
$r .= '</tr>' . "\r\n";
}
/* Close table body */
$r .= '</tbody>' . "\r\n";
/* if footer is required */
if (isset($attributes['footer']) && $attributes['footer']==true) {
$r .= '<tfoot>' . "\r\n";
$r .= '<tr>' . "\r\n";
foreach ($heads AS $head) {
$r .= '<th>';
if (self::checkArray($head)) {
$r .= $head['title'];
} else {
$r .= $head;
}
$r .= '</th>' . "\r\n";
}
$r .= '</tr>' . "\r\n";
$r .= '</tfoot>' . "\r\n";
}
/* Close the table */
$r .= '</table>';
/* Close responsive container if present */
if (isset($attributes['responsive']) && $attributes['responsive'] == true)
$r .= '</div>' . "\r\n";
return $r;
}
return null;
}
/**
* Check the $data param for type and size
*
* @param null $data
*
* @return bool
*/
public static function checkArray($data = null) {
if (is_array($data) AND count($data)>0) {
return true;
}
return false;
}
public static function generatePreview() {
/* Column names for easier naming, possibility of gettext use */
$heads['index1'] = 'Index 1 title';
$heads['index2'] = 'Index 2 title';
$heads['index3'] = 'Index 3 title';
$heads['index4'] = 'Index 4 title';
/* the content is in a associative array, as like a PDO result fetch with the PDO::FETCH_ASSOC option */
$data[] = array(
'index1'=>'Value 1',
'index2'=>'Value 2',
'index3'=>'Value 3',
'index4'=>'Value 4'
);
$data[] = array(
'index1'=>'Value 1',
'index2'=>'Value 2',
'index3'=>'Value 3',
'index4'=>'Value 4'
);
$data[] = array(
'index1'=>'Value 1',
'index2'=>'Value 2',
'index3'=>'Value 3',
'index4'=>'Value 4'
);
$data[] = array(
'index1'=>'Value 1',
'index2'=>'Value 2',
'index3'=>'Value 3',
'index4'=>'Value 4',
'attr'=> array(
'style'=>'font-style: italic; color: red;'
)
);
echo self::createTable($heads,$data,array(
'footer'=>true
));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment