我的状态表有state_id和state_name。我将它直接显示在模板文件中,如下所示..
<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>';
}
}
}
答案 0 :(得分:1)
根据之前评论中的讨论,我猜该属性是您网站中的自定义帖子类型。因此,如果这是情景,那么这应该是解决方案。
<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>
尝试此操作,如果您遇到任何问题,请与我们联系。
答案 1 :(得分: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>
只需将SELECTED_STATE_ID替换为包含所选状态的变量