Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by milosh )

HTML Table Library helper extension

Category:Contributions -> Libraries -> Table
Here is a little extension that I wrote to table library, with the following functions

add_column <- works similarly to add_row, but adds column
transpose <- transposes a table (rows become columns and columns become rows)
remove_column <- removes column
add_anchor <- adds an anchor to the table.

I wrote it mainly because I wanted to have a simple method to add links to each row of table, which has been built from database query. This works now with two commands:

$this->table->add_anchor($id_column_name, $value_column_name, $path);
$this->table->remove_column($id_column_name);

Usage examples:

<?php
        $this
->table->add_row(array('John''red''small'));
        
$this->table->add_row(array('Mary''blue''medium'));
        
$this->table->add_row(array('Jim''green''large'));
        
$this->table->add_row(array('Peter''black''unknown'));
        
        
$this->table->set_heading(array('name''colour''size'));
        
        echo 
$this->table->generate();
        echo 
"--------------";
        
        
// Add some columns. Note that you can either add a column as array or as list
        // If table heading is set, then the first name is assumed to be heading. 
        
$this->table->add_column('availability''free''unavailable''available''later');
        
$this->table->add_column(array('another''first''second''third''fourth')); 
        echo 
$this->table->generate();
        echo 
"--------------";

        
// Create a link for "size" column on each row that links to path "part/delete/$name"
        // The search is done by the name of the column, so be aware of case sensitivity
        // 
        // The function below adds a link <a href="http://www.example.com/index.php/part/delete/$name">$size</a> to each row of the "size" column. 
        //
        // the function syntax is add_anchor($id_value_colum_name, $value_column_name, $path)
        
$this->table->add_anchor('name''size''part/delete');
        echo 
$this->table->generate();
        echo 
"--------------";
        
        
// Now hide the "name" column. Note that links are still working.
        // Note that this gives you a nice way to build links from database query ID columns
        // and hide ID columns from viewing. :)
        
$this->table->remove_column('name');
        echo 
$this->table->generate();
        echo 
"--------------";
        
        
// Transpose a table
        
$this->table->transpose();
        echo 
$this->table->generate();
        echo 
"--------------";
?> 

The code to add into your Library folder:

<?php 
/**
 * Some helpers
 * 
 * add_column
 * remove_colum
 * add_anchor($id_column_name, $value_column_name, $path)
 * transpose
 * 
 *
 */
Class MY_Table extends CI_Table {
    
    
public $columns;
    
    
/**
     * Transpose array
     * 
     * @param $array
     * @return array
     */

    /**
     * Add column to table 
     * 
     * @return unknown_type
     */
    
function add_column(){
        
// Is heading empty?
        
if (empty($this->heading)){
            
// no heading yet
            
$columns $this->_flip($this->rows);
            
$args func_get_args();
            
$columns[] = (is_array($args[0])) ? $args[0] $args;
            
$this->rows $this->_flip($columns);
        
else 
            
// Assume that the first element is heading
            
$args func_get_args();
            
$col = (is_array($args[0])) ? $args[0] $args;
            
$heading array_shift($col);
            
// add columns...
            
$columns $this->_flip($this->rows);
            
$columns[] $col;
            
$this->rows $this->_flip($columns);
            
// ...and heading
            
$this->heading[] $heading;
        
}
    }
    
    
// Remove column from table
    
function remove_column($name){
        
if(empty($this->heading))
            return;
        
$key array_search($name$this->heading);
        
$columns $this->_flip($this->rows);
        unset(
$columns[$key]);
        unset(
$this->heading[$key]);
        
$this->rows $this->_flip($columns);
    
}
    
    
// Transpose table
    
function transpose(){
        
if (!empty($this->heading))
            
$this->heading = array(); // Empty heading
        
$this->rows $this->_flip($this->rows);
    
}
    
    
// Create anchor for table row
    
function add_anchor($id_column$value_column$path){
        $CI 
= & get_instance();
        
$CI->load->helper('url');
        if (empty(
$this->heading))
            return;
        
$id_key array_search($id_column$this->heading);
        
$val_key array_search($value_column$this->heading);
        foreach (
$this->rows as $key => $var{
            $this
->rows[$key][$val_key] $this->_add_anchor($this->rows[$key][$val_key]$this->rows[$key][$id_key]$path);
        
}
    }
    
    
private function _add_anchor($value$id$path){
        
return anchor($path"/" .$id$value);
    
}
    
    
private function _flip($array){
        $flipped 
= array();
        foreach (
$array as $key => $subarray){
            
foreach ($subarray as $subkey => $subvar{
                $flipped[$subkey][$key] 
$subvar;
            
}
        }
        
return $flipped;
    
}