Welcome Guest, Not a member yet? Register   Sign In
How to get enum values
#1

[eluser]Unknown[/eluser]
I've just started using CI but I've ran into a problem handling enum, just wondering if anyone has any suggestions?

I have some fields in the database (mysql) that are set as enum e.g. platform. Is there a function to get the values of these enum ?

I had previously used code like this but I was wonder if CI had a nicer way to do it?

$query="SHOW COLUMNS FROM hds_scripts LIKE 'Platform'";
$result=mysql_query($query);
if(mysql_num_rows($result)>0){
$row=mysql_fetch_row($result);
$options=explode("','",preg_replace("/(enum|set)\('(.+?)'\)/","\\2",$row[1]));
}
#2

[eluser]Chalda Pnuzig[/eluser]
I have the same problem...
Have you solved it?

[edit]
I found this function from http://akinas.com/pages/en/blog/mysql_enum :

Code:
function enum_select( $table , $field ){
    $query = " SHOW COLUMNS FROM `$table` LIKE '$field' ";
    $result = mysql_query( $query ) or die( 'error getting enum field ' . mysql_error() );
    $row = mysql_fetch_array( $result , MYSQL_NUM );
    #extract the values
    #the values are enclosed in single quotes
    #and separated by commas
    $regex = "/'(.*?)'/";
    preg_match_all( $regex , $row[1], $enum_array );
    $enum_fields = $enum_array[1];
    return( $enum_fields );
}

In CI:
Code:
function enum_select( $table , $field ){
    $query = " SHOW COLUMNS FROM `$table` LIKE '$field' ";
    $row = $this->db->query(" SHOW COLUMNS FROM `$table` LIKE '$field' ")->row()->Type;
    $regex = "/'(.*?)'/";
    preg_match_all( $regex , $row, $enum_array );
    $enum_fields = $enum_array[1];
    return( $enum_fields );
}
#3

[eluser]psychobob[/eluser]
[quote author="Chalda Pnuzig" date="1234999201"]I have the same problem...
Have you solved it?

[edit]
I found this function from http://akinas.com/pages/en/blog/mysql_enum :

Code:
function enum_select( $table , $field ){
    $query = " SHOW COLUMNS FROM `$table` LIKE '$field' ";
    $result = mysql_query( $query ) or die( 'error getting enum field ' . mysql_error() );
    $row = mysql_fetch_array( $result , MYSQL_NUM );
    #extract the values
    #the values are enclosed in single quotes
    #and separated by commas
    $regex = "/'(.*?)'/";
    preg_match_all( $regex , $row[1], $enum_array );
    $enum_fields = $enum_array[1];
    return( $enum_fields );
}

In CI:
Code:
function enum_select( $table , $field ){
    $query = " SHOW COLUMNS FROM `$table` LIKE '$field' ";
    $row = $this->db->query(" SHOW COLUMNS FROM `$table` LIKE '$field' ")->row()->Type;
    $regex = "/'(.*?)'/";
    preg_match_all( $regex , $row, $enum_array );
    $enum_fields = $enum_array[1];
    return( $enum_fields );
}
[/quote]


I adjusted your code like this:
Code:
function enum_select( $table , $field )
    {
        $query = "SHOW COLUMNS FROM ".$table." LIKE '$field'";
        $row = $this->query("SHOW COLUMNS FROM ".$table." LIKE '$field'")->row()->Type;
        $regex = "/'(.*?)'/";
        preg_match_all( $regex , $row, $enum_array );
        $enum_fields = $enum_array[1];
        foreach ($enum_fields as $key=>$value)
        {
            $enums[$value] = $value;
        }
        return $enums;
    }
and saved it inside of /system/database/DB_driver.php

to call the function I do
Code:
echo form_dropdown('test', $this->db->enum_select('admins','access_level'));
where 'admins' is the table name, and 'access level' is the enum_column name.

Hope this helps! I appreciate what you had, because that sure helped me Smile
#4

[eluser]LiorBroshi[/eluser]
Thanks...exactly what I needed ;-)

Small Fix:

This:
Code:
$row = $this->query("SHOW COLUMNS FROM ".$table." LIKE '$field'")->row()->Type;

should be
Code:
$row = $this->db->query("SHOW COLUMNS FROM ".$table." LIKE '$field'")->row()->Type;




Theme © iAndrew 2016 - Forum software by © MyBB