【问题标题】:How to display the state and city name from the database on edit page?如何在编辑页面上显示数据库中的州和城市名称?
【发布时间】:2018-09-15 04:12:57
【问题描述】:

我正在编辑页面中从数据库中获取数据。

我正在使用 CodeIgniter,我有两个视图页面,registeredit_register

我在register 页面上没有任何问题,但我仍在分享这个过程以了解我的问题。在此页面上,国家、州、城市的依赖性也在起作用。我的意思是我选择“印度”然后它将在状态下拉列表中显示所有状态名称。现在我选择“马哈拉施特拉邦”,然后选择“孟买”。完美运行。

我提交了表单并在数据库中,我得到了类似的值

 Country_name | state_name | city
 101          |  22        | 2707
--------------------------------------
    101-India
    22-Maharashtra
    2707-Mumbai

我们来谈谈edit_register页面。

现在我在edit_register 页面上,我在下拉列表中获取国家名称。我得到了正确的输出“印度”,但没有得到州和城市名称。

所以我的问题是如何从数据库中显示州和城市名称?

当我点击编辑按钮时请检查这个,然后我只得到国家名称但不显示州名。

如果我选择其他国家并再次选择印度,那么我会在下拉列表中获得所有州名。

控制器

它在下拉列表中显示我的编辑页面和国家/地区名称

public function search_with_number(){
  $c_mobileno=$this->input->post('c_mobileno_search');
  $got_customer_info['search_customer_info']=$this->Customer_model->get_customer_info($c_mobileno);
  $got_customer_info['get_country']=$this->Customer_model->get_country();// all country name
  $this->load->view('customer/search_order',$got_customer_info);
}

国家/地区下拉菜单可以使用。

<select  class="form_control country_change" name="c_s_country" data-target="target_state_dropdown2">
    <option value="" disabled selected>Select Country</option>
    <?php foreach ($get_country as $row) {?>
    <option <?php if($row->id == $post->c_s_country ){ echo 'selected="selected"'; } ?> value="<?php echo $row->id; ?>"><?php echo $row->country_name;?></option>
    <?php }?>
</select>

状态下拉菜单不起作用

<select  class="form_control state_get" name="c_s_state" id="target_state_dropdown2" data-target="city_dropdown2">
    <option value='' disabled selected>Select state</option>
    <!--What code I have to user here-->
</select>

城市不工作

<select  class="form_control city_get" name="c_s_city" id="city_dropdown2">
    <option value="" disabled selected>Select city </option>
    <!--What code I have to user here-->
</select>

我在注册页面中使用的 Ajax

    /*Get all the state name using country code*/
   $(".country_change").on('change',function(){
      var country_id=$(this).val();
      var select_list = $(this).data('target'); // The dropdown ID
  $.ajax({
      url:baseUrl +"/Customer_control/statename",
      method:"POST",
      data:"country_id="+country_id,
     dataType: "json",
      success:function(data){
        $('#'+select_list).empty();
        var placeholder="<option value='' disabled selected>Select state</option>";
        $('#'+select_list).html(placeholder);
            $.each(data, function(i, data) {
          $('#'+select_list).append("<option value='" + data.id + "'>" + data.state_name + "</option>");
            });
      }
     });
  });
   /*Get all the city name using state code*/
   $(".state_get").on('change',function(){
      var state_id=$(this).val();
      var select_list = $(this).data('target'); // The dropdown ID
  $.ajax({
      url:baseUrl +"/Customer_control/cityname",
      method:"POST",
      data:"state_id="+state_id,
      dataType: "json",
      success:function(data){
        $('#'+select_list).empty();
        var placeholder="<option value='' disabled selected>Select city</option><option value='Other'>Other</option>";
        $('#'+select_list).html(placeholder);
        $.each(data, function(i, data) {
        $('#'+select_list).append("<option value='" + data.id + "'>" + data.cities_name + "</option>");
            });
      }

     });
  });

状态模型

   public function get_country()
{
  $get_country = array('is_country_active' => 1);
    $this->db->where($get_country);
     $query = $this->db->get('countries');
      $result = $query->result();
      if($result)
      {
        return $result;
      }
      else 
      {
        return 0;
      } 
}

public function get_state($country_id){
  $get_state = array('is_state_active' => 1,'country_id'=>$country_id);
    $this->db->where($get_state);
     $query = $this->db->get('states');
      $result = $query->result();
      if($result)
      {
        return $result;
      }
      else 
      {
        return 0;
      } 
}
public function get_city($state_id){
  $get_city = array('is_city_active' => 1,'state_id'=>$state_id);
    $this->db->where($get_city);
     $query = $this->db->get('cities');
      $result = $query->result();
      if($result)
      {
        return $result;
      }
      else 
      {
        return 0;
      } 
}

我还没有添加城市JS。

现在请检查我的 Ajax 代码,当我选择国家名称时,我的州名正在改变。我必须显示来自数据库的州和城市名称 onload

【问题讨论】:

  • 您的$this-&gt;Customer_model-&gt;get_country(); 是否返回用户在注册时发布的州和城市?
  • @Nik,感谢您的回复,是的,注册时我得到了所有国家/地区的名称。仅返回国家/地区名称
  • 好的。那么您是否错过了选择用户的首选城市和州?您在哪里获取注册用户的城市和州?就像您有 Customer_model-&gt;get_country(); 来获取国家/地区一样吗?
  • 您的意思是在初始页面加载时不显示状态,还是在更改国家/地区时不显示?
  • 我不明白为什么您不能使用与国家相同的代码来获取州(当然选择不同)。只需从数据库中获取状态。如果我正确理解了您的代码,您已经将州名存储在数据库中。

标签: javascript php jquery html codeigniter-3


【解决方案1】:

在编辑视图中,您需要执行与国家/地区相同的操作。首先,您需要从 db 中获取您所有的州和城市。
如果您在创建过程中不存储 state_id 和 city_id ,还需要将其存储在 db 中。

现在您需要将这些数据传递到您的编辑视图,并像为国家/地区所做的那样做同样的事情:-

<select name = "state">
<option value="">Choose State</option>  
    <?php foreach($state as $row){ ?> 
          <option value="<?php echo $row->state_id; ?>"  <?php if($row->state_id == $selected->state_id){  echo "SELECTED";}  ?>> <?php echo $row->state_id; ?></option>  
    <?php } ?>    
</select>

对城市也做同样的事情。还请记住,如果用户更改城市,您也需要在编辑页面执行 ajax。

这应该是控制器上的代码:-

public function search_with_number(){
  $c_mobileno=$this->input->post('c_mobileno_search');
  $got_customer_info['search_customer_info']=$this->Customer_model- 
   >get_customer_info($c_mobileno); //get here your saved state id of this user and match it in the view level
  $got_customer_info['get_country']=$this->Customer_model->get_country();// all country name;
  $got_customer_info['get_state']=$this->State_model->get_state();// all state name;
  $this->load->view('customer/search_order',$got_customer_info);
}

【讨论】:

  • 感谢@Varun 的回复。我添加了状态模型代码来获取状态名称。我在注册页面中使用相同的模型脚本。我应该为此使用其他州模型代码吗?
  • 无需使用其他状态模型,但您需要获取用户在创建记录时选择的特定状态 ID。获得该记录后,将该州 ID 与您的所有州匹配,就像您已经为您的国家/地区所做的那样。
  • @VarunKumar,我正在获取特定的状态 ID。由于我传递了县 ID,我的 get_state 函数不起作用。
  • 只有当您从下拉列表中更改国家/地区时,您的 get_state 函数才会起作用。在此函数中,您需要添加以下行:- public function search_with_number(){ $got_customer_info['get_selected_state']=$this-&gt;Customer_model-&gt;get_customer_info($c_id); //get that id of state which user selected at creation $got_customer_info['get_state']=$this-&gt;Customer_model-&gt;get_state();// all state name }
  • 是的,如果我从下拉列表中更改国家/地区名称,get_state 函数将起作用,但我必须为 $this->Customer_model->get_state(); 编写一个新函数;对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 2020-02-18
  • 2011-03-23
相关资源
最近更新 更多