Database helper for CodeIgniter to retrieve enumerated field values

Load the helper:

$this->load->helper(‘database’);

Call the function:

$enums = field_enums(‘table_name’, ‘field_name’);

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('field_enums'))
{
    function field_enums($table = '', $field = '')
    {
        $enums = array();
        if ($table == '' || $field == '') return $enums;
        $CI =& get_instance();
        preg_match_all("/'(.*?)'/", $CI->db->query("SHOW COLUMNS FROM {$table} LIKE '{$field}'")->row()->Type, $matches);
        foreach ($matches[1] as $key => $value) {
            $enums[$value] = $value; 
        }
        return $enums;
    }  
}

/* End of file database_helper.php */
/* Location: ./application/helpers/database_helper.php */

Happy coding 🙂

3 thoughts on “Database helper for CodeIgniter to retrieve enumerated field values

Leave a Reply

Your email address will not be published. Required fields are marked *