Friday 16 September 2011

Extending Table Library in Codeigniter

Codeigniter has a great Table library to display data in html table format. Adding a Serial No is sometime required to a table, but Row num is not easily provided by MySQL hence while using Table library this function is added to do the job.

Here goes the code:
 /**  
    * Set the serial no  
    *  
    * Add serial no to left of table  
    *  
    * @access  public  
    * @param  mixed  
    * @return  mixed  
    */  
   function add_sr_no(&$arr)  
   {  if (!is_array($arr)) return $arr;  
     $i=1;  
     foreach ($arr as &$values) {        
       if (!is_array($values))  
         break;  
       else  
         array_unshift($values, $i++);  
     }      
     return $arr;  
   }  
Just add these lines to table.php in library.
Usage:
$rows = $query->result_array();
$this->table->add_sr_no($rows);
Don't forget to add first column 'Sr No' in set_heading.

Hope this helps.


Views are invited.

1 comment: