jenniferG Posted October 21, 2007 Share Posted October 21, 2007 Is there anyway in PHP 4 to create an array containing the enumerated values of a column?. ie: columna enum('val1','val2','val3'); we want to generate an array where: array[0]='val1' array[1]='val2' array[3]='val3' Thanks, Jennifer Quote Link to comment https://forums.phpfreaks.com/topic/74213-enumeraterd-values/ Share on other sites More sharing options...
wildteen88 Posted October 21, 2007 Share Posted October 21, 2007 you could do: //$search_value = 'val'; // doesn't work $search_value = 'val2'; // works $enum = array('val1', 'val2', 'val3'); if(in_array($search_value, $enum)) { echo $enum[array_shift(array_keys($enum, $search_value))]; } else { echo "'$search_value' not found"; } Quote Link to comment https://forums.phpfreaks.com/topic/74213-enumeraterd-values/#findComment-374851 Share on other sites More sharing options...
jenniferG Posted October 21, 2007 Author Share Posted October 21, 2007 I may have phrased this wrong. I have a column [columnA] defined in a table as enum(...................); what I want to do is query the database and get back an array containing the enumerated values attrributed to that specific column. Jennifer Quote Link to comment https://forums.phpfreaks.com/topic/74213-enumeraterd-values/#findComment-375017 Share on other sites More sharing options...
wildteen88 Posted October 21, 2007 Share Posted October 21, 2007 Oh sorry misunderstood your question. Did a quick search on google and found this function: <?php // connect to mysql here 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 ); } $table = 'tbl_name_here'; $field = 'field_name_here_that_contains_enum'; $enum_values = enum_select($table, $field); // display possible enum values from enum field. echo '<pre>' . print_r($enum_values, true) . '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74213-enumeraterd-values/#findComment-375043 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.