martinpompey Posted August 13, 2010 Share Posted August 13, 2010 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 More sharing options...
PFMaBiSmAd Posted August 13, 2010 Share Posted August 13, 2010 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. Link to comment https://forums.phpfreaks.com/topic/210642-converting-an-int-to-a-string/#findComment-1098852 Share on other sites More sharing options...
martinpompey Posted August 13, 2010 Author Share Posted August 13, 2010 Thank you so much PFMaBiSmAd, I am going to try and implement this straight away and see if I can get it working. Thank you so much for your quick answer :-) Link to comment https://forums.phpfreaks.com/topic/210642-converting-an-int-to-a-string/#findComment-1098856 Share on other sites More sharing options...
martinpompey Posted August 13, 2010 Author Share Posted August 13, 2010 Thanks a lot, it worked first time (of course). Thanks so much for your help. This also will help me understand arrays a bit more too!!! Link to comment https://forums.phpfreaks.com/topic/210642-converting-an-int-to-a-string/#findComment-1098861 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.