vtx-rider Posted September 21, 2008 Share Posted September 21, 2008 Hi @all Is there a possibility to simplify this code? <?php if ($result['f6'] == Schweiz) { echo "<img src='images/ch.gif' alt='Schweiz'>"; } if ($result['f6'] == Deutschland) { echo "<img src='images/deutschland.jpg' alt='Deutschland'>"; } if ($result['f6'] == Österreich) { echo "<img src='images/oesterreich.jpg' alt='Österreich'>"; } if ($result['f6'] == Niederlande) { echo "<img src='images/nl.gif' alt='Niederlande'>"; } if ($result['f6'] == Italien) { echo "<img src='images/italien.jpg' alt='Italien'>"; } if ($result['f6'] == Frankreich) { echo "<img src='images/frankreich.jpg' alt='Frankreich'>"; } if ($result['f6'] == USA) { echo "<img src='images/usa.jpg' alt='USA'>"; } if ($result['f6'] == Belgien) { echo "<img src='images/belgien.gif' alt='Belgien'>"; } ?> The Code works, but I think, that it is possible to make it shorter. Thanks! Link to comment https://forums.phpfreaks.com/topic/125186-solved-simplify-code/ Share on other sites More sharing options...
pocobueno1388 Posted September 21, 2008 Share Posted September 21, 2008 You can use a switch statement. It's really not going to shorten your code much, but it was made for situations like this. Link to comment https://forums.phpfreaks.com/topic/125186-solved-simplify-code/#findComment-647081 Share on other sites More sharing options...
PFMaBiSmAd Posted September 21, 2008 Share Posted September 21, 2008 You could use an array where each language is the key and the resultant string is the value. After you validate that $result['f6'] is a valid key value, just echo your_array_name[$result['f6']]; Link to comment https://forums.phpfreaks.com/topic/125186-solved-simplify-code/#findComment-647098 Share on other sites More sharing options...
PFMaBiSmAd Posted September 21, 2008 Share Posted September 21, 2008 To add more languages, just add its' definition as an array entry - <?php $language_arr = array(); $language_arr['Schweiz'] = "<img src='images/ch.gif' alt='Schweiz'>"; $language_arr['Deutschland'] = "<img src='images/deutschland.jpg' alt='Deutschland'>"; $language_arr['Österreich'] = "<img src='images/oesterreich.jpg' alt='Österreich'>"; $language_arr['Niederlande'] = "<img src='images/nl.gif' alt='Niederlande'>"; $language_arr['Italien'] = "<img src='images/italien.jpg' alt='Italien'>"; $language_arr['Frankreich'] = "<img src='images/frankreich.jpg' alt='Frankreich'>"; $language_arr['USA'] = "<img src='images/usa.jpg' alt='USA'>"; $language_arr['Belgien'] = "<img src='images/belgien.gif' alt='Belgien'>"; if(array_key_exists($result['f6'],$language_arr)) { echo $language_arr[$result['f6']]; } else { echo "Language: {$result['f6']} is not supported"; } ?> Link to comment https://forums.phpfreaks.com/topic/125186-solved-simplify-code/#findComment-647105 Share on other sites More sharing options...
vtx-rider Posted September 21, 2008 Author Share Posted September 21, 2008 Thank you for your help! Both solutions work well. Link to comment https://forums.phpfreaks.com/topic/125186-solved-simplify-code/#findComment-647179 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.