Jump to content

[SOLVED] Simplify Code


vtx-rider

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

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