Jump to content

Recommended Posts

Hi All,

 

I have a php form which is posting a country variable which is currently a number value, but before I post this to a flat text file I want to convert the number into a string (E.G. in the DB 1 = France 3=UK 6-Germany)... I was thinking something along the lines of:

 

$Country = $_REQUEST['Country'];

 

if ($Country =1)

{$CountryW = 'France';}

 

else if ($Country =3)

{$Country = 'UK';}

 

else if ($Country =9)

{$Country = 'Test';}

 

else {$Country = 'Germany';}

 

But this doesn't seem to be working and there would be around 180 Countries so I can see its not efficient. Any help would be much appreciated. If you need more code let me know.

 

Thanks so much for any help

Martin

Link to comment
https://forums.phpfreaks.com/topic/210642-converting-an-int-to-a-string/
Share on other sites

You would use a lookup array -

 

$lookup = array();
$lookup[1] = 'France';
$lookup[3] = 'UK';
$lookup[9] = 'Test';
// add other entries here ...

$Country = $_REQUEST['Country'];

if(isset($lookup[$Country])){
    $Country = $lookup[$Country];
} else {
    $Country = 'Germany';
}

 

Once you have an array like that you would also use it to build your form so that you have consistency and shorter code.

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.