Problem mit Schleife

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Problem mit Schleife

    Hab folgendes Problem:
    Ich möchte in meiner User-Verwaltung für die User ein Feature bereitstellen mit dem sie ihr Profil ändern können, das ist auch nicht das Problem, nur bei der Länderauswahl komm ich ins stocken.
    Hier erstmal wie ich das beim Geburtstag gelöst habe:
    PHP-Code:
    <?php
      
    echo "<select name='geb_tag'>";
      for(
    $i=1$i<=31$i++) {
        if ( 
    $i == $geb_tag ) {
          echo 
    "<option selected>".$i."</option>";
        }
        else {
          echo 
    "<option>".$i."</option>";
        }
      }
      echo 
    "</select>";
    ?>
    Das gibt mir eine schöne Auswahlliste der Tage von 1 bis 31 und der Tag der sich in der Datenbank befindet (also vom User schon einmal eingegeben wurde) ist schon markiert.
    So soll das nun auch bei der Herkunftslandauswahl sein.
    PHP-Code:
    <?php
      
    echo "<select name='land'>";
      if ( 
    $land == "deu" ) {
        echo 
    "<option value='deu' selected>Deutschland</option>";
        echo 
    "<option value='ch'>Schweiz</option>";
        
    // und so weiter
      
    } elseif ( $land == "ch" ) {
        echo 
    "<option value='deu'>Deutschland</option>";
        echo 
    "<option value='ch' selected>Schweiz</option>";
        
    // und so weiter
      
    }
        echo 
    "</select>";
    ?>
    Das ist ja wohl etwas aufwändig für alle zu machen und wenn mal ein Land hinzukommt wird es auch blöd -.-
    Kann mir wer helfen wie ich da ne Schleife draus formulier?
    Mess with the Besth, die like the rest!

  • #2
    Re: Problem mit Schleife

    Du kennst Arrays?

    Fein, nutze sie.
    I don't believe in rebirth. Actually, I never did in my whole lives.

    Kommentar


    • #3
      PHP-Code:
      $land 'res der db abfrage z.B.deu';
      $check = array('deu'=>'Deutschland','ch'=>'Schweiz');
          foreach(
      $check as $key=>$value){
              if(
      $land == $key){
                  echo 
      '<option value="'.$key.'"selected="selected">'.$value.'</option>';
              }else{
                   echo 
      '<option value="'.$key.'">'.$value.'</option>';
              }

      So in der Art

      Gruss

      tobi
      Gutes Tutorial | PHP Manual | MySql Manual | PHP FAQ | Apache | Suchfunktion für eigene Seiten

      [color=red]"An error does not become truth by reason of multiplied propagation, nor does truth become error because nobody sees it."[/color]
      Mohandas Karamchand Gandhi (Mahatma Gandhi) (Source)

      Kommentar


      • #4
        ah danke
        hätt ich auch selbst drauf komm könn
        danke!
        Mess with the Besth, die like the rest!

        Kommentar

        Lädt...
        X