Jump to content

enumeraterd values


jenniferG

Recommended Posts

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";
}

Link to comment
https://forums.phpfreaks.com/topic/74213-enumeraterd-values/#findComment-374851
Share on other sites

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>';

?>

Link to comment
https://forums.phpfreaks.com/topic/74213-enumeraterd-values/#findComment-375043
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.