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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.