2012-03-26 9 views
-3

Ich habe eine Liste der Länder, in diesem Format in einer externen Datei: ‚AF:PHP-Array zu HTML wählt

<? $countries=array(); 
$countries['AF']='Afghanistan'; 
$countries['AL']='Albania'; 
$countries['DZ']='Algeria'; 
$countries['AS']='American Samoa'; 
$countries['AD']='Andorra'; 
$countries['AO']='Angola'; ?> 

Wie kann ich sie Liste als Optionen in einem HTML wählen, mit dem abreviation (ex machen ') der Wert und der vollständige Name des Landes, der angezeigte Text?

Ex: <option value="AF">Afghanistan</option>

+9

RTFM: http://php.net/foreach? –

+0

Was hast du probiert? Was hast du gelesen? Dies ist so einfach wie 'foreach ($ Länder als $ abbrv => $ country) {// echo}'. – Shef

+0

[Was haben Sie versucht] (http://mattgemamm.com/2008/12/08/what-have-you-tried/)? – ghoti

Antwort

9
foreach($countries as $cc => $name) { 
    echo '<option value="' . $cc . '">' . $name . '</option>'; 
} 

Wenn die externe Datei, um die Länderliste enthält nur (btw, verwenden <?php statt <? !!), einfach include es.

+0

Warum

+0

Nicht alle Umgebungen unterstützen Daniel

+1

Die ' Graham

3
<select> 
<?php 
    foreach($countries as $id=>$name) 
     echo "<option value=\"$id\">$name</option>"; 
?> 
</select> 
+1

Fluchtorgien sind hässlich; Sowohl PHP als auch HTML funktionieren gut mit '' '' '' ' – ThiefMaster

+0

' echo ''; 'wäre erstaunlich. Es würde Anfänger machen Scripter rollen ihre Augen.: D – Shef

3
foreach ($countries as $key => $value) { 
    printf("\t<option value='%s'>%s</option>\n", $key, $value); 
}