2014-09-11 127 views
0

我的狀態表有state_id和state_name。我直接在像下面的模板文件顯示它..如何在wordpress中顯示下拉菜單中的選定值

<select name="state" id="state" class="select-submit2"> 
    <option value="">Select state</option> 
    <?php 
    $result=$wpdb->get_results("select * from states"); 
    foreach($result as $row) { 
     $state_id=$row->state_id; 
     $state_name=$row->state_name; 
     echo '<option value='.$state_id.'>'.$state_name.'</option>'; 
    } 
    ?>  
</select> 

但是,當我想編輯如何顯示第一選擇狀態的名稱。????

編輯頁面的網址... whitecode.in/demo/plotsup_plot/new-property/?listing_edit=6795

這是function.php文件我的功能代碼

function getcity(){ 
     global $wpdb; 
     if($_POST['state']) 
      { 
       $id=$_POST['state']; 
       $property_id = $_GET['listing_edit']; 
            $district = get_post_meta($property_id, district, true); 
       $result=$wpdb->get_results("SELECT * FROM districts WHERE state_id='$id'"); 
       //$wpdb->get_results($query); 
           foreach($result as $row) { 
                  $city_name = $row->district_name; 
          $city_id  = $row->district_id; 
         ?> 
    <option value="<?php echo $city_id; ?>" <?php if($district == $city_id){ echo 
    'selected="selected"';} ?>><?php echo $city_name; ?></option> 
    <?php  
           //echo '<option value="'.$city_id.'">'.$city_name.'</option>'; 


      } 
    } 
    } 

回答

1

按照在之前的評論中討論變量SELECTED_STATE_ID,我想這屬性是在您的網站自定義後的類型。所以如果這是場景,那麼這應該是解決方案。

<select name="state" id="state" class="select-submit2"> 
<option value="">Select state</option> 
<?php 
$property_id = $_GET['listing_edit']; //the PROPERTY_ID should be replaced with the original id might be ina get variable or any process by which you are using for the edit page. 
$property_state = get_post_meta($property_id, META_KEY_STATE, true); //META_KEY_STATE is the meta_key name you use to store the value of the state in the postmeta table 
$result=$wpdb->get_results("select * from states"); 
foreach($result as $row) { 
    $state_id=$row->state_id; 
    $state_name=$row->state_name; 
    ?> 
    <option value="<?php echo $state_id; ?>" <?php if($property_state == $state_id){ echo 'selected="selected"';} ?>><?php echo $state_name; ?></option> 
    <?php 
} 
?>  
</select> 

試試這個,讓我知道你是否面臨任何問題。

+0

property_id是我的post_id。 $ state是我的meta_key。但是,我如何獲得post_id? – 2014-09-11 10:10:57

+0

你可以在這裏發佈編輯頁面的網址,然後我可以理解獲取該ID的方式。 – suvajit 2014-09-11 10:14:03

+0

看到我上面的編輯... – 2014-09-11 10:16:16

0

如果這你正在尋找什麼?

<select name="state" id="state" class="select-submit2"> 
    <option value="">Select state</option> 
    <?php 
    $result=$wpdb->get_results("select * from states"); 
    foreach($result as $row) { 
     $state_id=$row->state_id; 
     $state_name=$row->state_name; 
     if($state_id == SELECTED_STATE_ID) 
      echo '<option value='.$state_id.' selected>'.$state_name.'</option>'; 
     else 
      echo '<option value='.$state_id.'>'.$state_name.'</option>'; 
    } 
    ?>  
</select> 

只需更換與包含狀態下選擇

+0

這並沒有工作..但它不是你的錯.. – 2014-09-11 07:44:29

+0

應該工作,所有你需要做的是用包含選定狀態ID的變量替換SELECTED_STATE_ID – 2014-09-11 07:47:15

+0

上面的代碼是在我的wordpress模板文件。那麼我從哪裏帶來包含所選狀態的Id的變量.. – 2014-09-11 07:52:02

相關問題