简体   繁体   English

如何在Wordpress中显示下拉菜单中的选定值

[英]How to display selected value from dropdown in wordpress

I have state table having state_id and state_name. 我有状态表具有state_id和state_name。 I am displaying it directly in template file like below.. 我直接在如下所示的模板文件中显示它。

  <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> 

But when I want to edit that how do I show first selected state name.???? 但是,当我要编辑时,如何显示第一个选定的州名。

Edit page url... whitecode.in/demo/plotsup_plot/new-property/?listing_edit=6795 编辑页面网址... whitecode.in/demo/plotsup_plot/new-property/?listing_edit=6795

This is my function code in function.php file 这是我在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>';


            }
   } 
   }

As per as discussed in the previous comments, I guess that property is a custom post type in your site. 按照前面的评论中的讨论,我猜该属性是您网站中的自定义帖子类型。 So if that is the scenario then this should be the solution. 因此,如果是这种情况,那么这应该是解决方案。

<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> 

Try this and let me know if you face any issues. 试试这个,让我知道您是否遇到任何问题。

If this is what you are looking for? 如果这是您想要的?

<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> 

just replace SELECTED_STATE_ID with variable which contains which state is selected 只需将SELECTED_STATE_ID替换为包含选择了哪个状态的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从下拉列表中检索选定的值并将其显示在同一个下拉框中 - How to retrieve the selected value from dropdown and display it in the same dropdown box 如何在数据库中的引导多重下拉列表中显示选定的值 - How to display selected value in the bootstrap multiple dropdown from the database 如何在国家/地区下拉列表中显示选定的值 - How to display selected value in country dropdown list 如果在第一个下拉菜单中选择了某个值,如何显示第二个下拉菜单? - How to display a second dropdown menu if a certain value in the first dropdown is selected? 仅当从下拉菜单中选择值时才显示结果 - display result from dropdown menu only when value from it is selected 如何从选定的图标下拉列表中获取价值? - How to get value from selected icon dropdown? 如何使用php的下拉列表框中的选定值显示db中的特定字段 - how to display specific fields from db using the selected value in dropdown listbox in php Codeigniter下拉列表不显示从数据库中选择的值 - Codeigniter dropdown doesn't display the selected value from database 如何在PHP重定向页面中显示下拉列表和复选框选择的值以进行确认? - How to display the dropdown & checkbox selected value in redirected page for confirmation IN PHP? 如何在带有选定值的for循环中显示多个下拉列表 - How can i display multiple dropdown list in for loop with selected value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM