Matt
Matt

Reputation: 13

Stored ENUM is displayed as a string

I have an ENUM stored in PHPMYADMIN which allows the numbers 1-10.

I'm trying to find out how that number can be converted to a string which the user can see, an example is;

1=London

2=Spain

3=France

4=Germany

etc...

The obvious way would be to do an if statement for each something like

if ENUM == 1 then STRING == "London"
if ENUM == 2 then STRING == "Spain"

but I was wondering if there was a similar way of doing this or if I just need to do 10 if statements. I've tried to look online but no helpful tutorials. Thanks (Sorry i've had to submit the question as code, stackoverflow wouldnt allow me to post it otherwise for some reason)

Upvotes: 1

Views: 359

Answers (1)

Spoody
Spoody

Reputation: 2882

Here is an efficient/clean/professional way of doing it:

$enum = 1; // The value fetched from the database

$cities = array(
    '1'=>'London', 
    '2'=>'Spain', 
    '3'=>'France', 
    '4'=>'Germany'
); // Array of cities
// Make sure there is a city with the given key
if(isset($cities[$enum])){
    echo $cities[$enum];
}

But it is also advisable to store the cities in another database table.

Upvotes: 2

Related Questions