2016-04-07 179 views
0

即時通訊新的PHP和im試試這個東西幾個小時,我想顯示數組項目的選擇選項。下面的代碼:從數組列表中選擇選項

<select name="state"> 
     <?php 
      $arrstate=array 
      (
      array("AK","Alaska"), 
      array("AL","Alabama"), 
      array("AR","Arkansas"), 
      array("AZ","Arizona"), 
      array("CA","California"), 
      array("CO","Colorado"), 
      array("CT","Connecticut"), 
      array("DC","District of Columbia"), 
      array("DE","Delaware"), 
      array("FL","Florida"), 
      array("GA","Georgia"), 
      array("HI","Hawaii"), 
      array("IA","Iowa"), 
      array("ID","Idaho"), 
      array("IL","Illinois"), 
      array("IN","Indiana"), 
      array("KS","Kansas"), 
      array("KY","Kentucky"),  
     ); 
      for($lop=0;$lop<=49;$lop++) 
       { 
       if (strtoupper($lead_info['state'])==$arrstate[$lop][0]) 
       { 
       echo "<option selected=\"selected\" value=\"".$arrstate[$lop][0]."\">".$arrstate[$lop][1]."</option>\n";   
       }else{ 
       echo "<option value=\"".$arrstate[$lop][0]."\">".$arrstate[$lop][1]."</option>\n";   
       } 
       } 
      ?> 
     </select> 

但似乎那隻能說明".$arrstate[$lop][1]." 出了什麼問題?

+0

爲什麼不使用關聯數組? –

+0

我想知道用戶是什麼狀態。在此頁面之前。有一個預先註冊,只有他/她的ZIP。我想根據他輸入的ZIP來顯示他/她目前的狀態。 – Kiel

+0

你可以做一個條件選項..例如'

回答

1
<?php 
     $arrstate=array 
     (
     "AK","Alaska", 
     "AL","Alabama", 
     "AR","Arkansas", 
     "AZ","Arizona" 
    ); 
    ?> 

這是最簡單的方法...

<select> 
     <?php 
      foreach($arrstate as $key => $value) { 
     ?> 
      <option value="<?php echo $key; ?>" <?=($value == $current_zip_entered ? "selected" : "")?>><?php echo $value; ?></option> 
     <?php 
      } 
      ?> 
    </select> 
1

嘗試

<?php 
    $arrstate = array 
    (
    'AK' => 'Alaska', 
    'AL' => 'Alabama', 
    'AR' => 'Arkansas', 
    'AZ' => 'Arizona' 
); ?> 
<select> 
    <?php foreach($arrstate as $key => $value) { ?> 
     <option value="<?php echo $key; ?>" <?=($value == $current_zip_entered ? "selected" : "")?>><?php echo $value; ?></option> 
    <?php } ?></select>