Php 在codigniter中编辑期间,无法显示数据库中的状态、城市名称
社区成员大家好 我在codeigniter中也面临同样的问题。在编辑页面上,我试图根据注册页面中选择的国家名称检索州和城市名称。我已经从数据库中获取选定的国家名称,但我无法根据国家名称从数据库中获取州和城市名称,因此请帮助我解决此问题 控制器代码Php 在codigniter中编辑期间,无法显示数据库中的状态、城市名称,php,html,mysql,Php,Html,Mysql,社区成员大家好 我在codeigniter中也面临同样的问题。在编辑页面上,我试图根据注册页面中选择的国家名称检索州和城市名称。我已经从数据库中获取选定的国家名称,但我无法根据国家名称从数据库中获取州和城市名称,因此请帮助我解决此问题 控制器代码 public function edit($id){ $user_data['user_data'] = $this->EditUser->getUser($id); $user_data['countries']=$th
public function edit($id){
$user_data['user_data'] = $this->EditUser->getUser($id);
$user_data['countries']=$this->Dynamic_model->fetch_country();
$user_data['states']=$this->Dynamic_model->fetch_state();
$user_data['cities']=$this->Dynamic_model->fetch_city();
$this->load->view('edit',$user_data);
}
public function update(){
$u_id=$this->input->post('user_id');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[15]',
array(
'required'=>'You Have not entered %s',
'min_length'=>'%s can"t be less than 5 characters',
'max_length'=>'%s can"t be more than 15 characters',
)
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('gender', 'Gender', 'required');
$this->form_validation->set_rules('checkbox','...','required',
array('required'=>'you need to agree with Terms & Conditions')
);
if($this->input->post('update')) {
if ($this->form_validation->run() == FALSE)
{
$this->edit($u_id);
}else
{
if(!empty($_FILES['userfile']['name'])){
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpeg|jpg|png';
$config['max_size'] = 2048;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = $this->upload->data();
$image_path=base_url('uploads/'.$data['file_name']);
$img=$data['file_name'];
}
}else{
$img=$this->input->post('old');
}
$username=$this->input->post('username');
$firstname=$this->input->post('firstname');
$lastname=$this->input->post('lastname');
$email=$this->input->post('email');
$gender=$this->input->post('gender');
$country=$this->input->post('country');
$state=$this->input->post('state');
$city=$this->input->post('city');
$editUser=array(
'user_name' => $username,
'fname' => $firstname,
'lname' => $lastname,
'eml' =>$email,
'gnd'=>$gender,
'img'=>$img,
'ctry' => $country,
'st' => $state,
'cty'=>$city,
);
$result = $this->EditUser->updateUser($u_id,$editUser);
if($result == true){
redirect('home/userdetails','refresh');
}else{
echo "updating records failed";
}
}
}
}
获取国家、州、城市的模型
class Dynamic_model extends CI_Model
{
public function fetch_country(){
$query = $this->db->get("countries");
$this->db->select('country_id','country_name');
return $query->result();
}
public function fetch_state($country_id){
$this->db->where('country_id',$country_id);
$query = $this->db->get('states');
return $query->result();
}
public function fetch_city($state_id){
$this->db->where('state_id', $state_id);
$query = $this->db->get('cities');
return $query->result();
}
}
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Edit User Details</title>
<link href="<?php echo base_url();?>/assets/css/bootstrap.min.css" rel="stylesheet">
<link href="<?php echo base_url();?>/assets/css/style.css" rel="stylesheet">
</head>
<body>
<!-- style="background:url(<?php echo base_url();?>/assets/css/Wireframe.png);" -->
<!-- <?php echo validation_errors(); ?> -->
<div class="container-sm" style="margin-top:20px;margin-bottom:20px;border:1px solid black;border-radius:10px;max-width:500px;background-color:black;border:2px solid #3498db;">
<div class="row">
<h1 style="margin-top:12px;color:white;font-weight:500;" class="text-center mb-1">Edit User Details</h1>
<?php echo form_open_multipart('home/update'); ?>
<div>
<input type="hidden" name="user_id" value="<?php echo $user_data->user_id ?>">
<input style="border:0;background:none;text-align:center;margin:14px auto;border:2px solid #3498db;border-radius:20px;width:400px;color:white;"
type="text"
value="<?= $user_data->user_name ?>"
name="username"
class="form-control"
placeholder="Username">
<div class="error">
<?php echo form_error('username'); ?>
</div>
</div>
<div>
<input style="border:0;background:none;text-align:center;margin:14px auto;border:2px solid #3498db;border-radius:20px;width:400px;color:white;"
name="email"
type="email"
value="<?= $user_data->eml ?>"
class="form-control"
placeholder="example@example.com">
<div class="error">
<?php echo form_error('email'); ?>
</div>
</div>
<div>
<input style="border:0;background:none;text-align:center;margin:14px auto;border:2px solid #3498db;border-radius:20px;width:400px;color:white;"
name="firstname"
type="text"
value="<?= $user_data->fname ?>"
class="form-control"
placeholder="First Name">
<div class="error">
<?php echo form_error('firstname'); ?>
</div>
</div>
<div>
<input style="border:0;background:none;text-align:center;margin:14px auto;border:2px solid #3498db;border-radius:20px;width:400px;color:white;"
name="lastname"
type="text"
value="<?= $user_data->lname ?>"
class="form-control"
placeholder="Last Name">
<div class="error">
<?php echo form_error('lastname'); ?>
</div>
</div>
<div style="border:0;background:none;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;">
<input style="margin-left: 10px;" class="form-check-input" type="radio" name="gender" value="Male" <?php if ($user_data->gnd=="Male") echo "checked"; ?>> Male
<input style="margin-left:20px;"class="form-check-input" type="radio" name="gender" value="Female" <?php if ($user_data->gnd=="Female") echo "checked"; ?>> Female
</div>
<div class="error">
<?php echo form_error('gender'); ?>
</div>
<div style="margin-left:34px;">
<select style="border:0;background:black;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;" name="country" id="country-dropdown" class="form-item">
<?php
foreach ($countries as $user_ctry){ ?>
<option value="<?= $user_ctry->country_id?>"<?=($user_data->ctry == $user_ctry->country_id ? 'selected="selected"':'')?>><?=$user_ctry->country_name?> </option>
<?php }?>
</select>
</div>
<div style="margin-left:34px;">
<select style="border:0;background:black;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;" name="state" id="state-dropdown" class="form-item">
<?php foreach($states as $user_st){ ?>
<option value="<?php echo $user_st->state_id?>"<?=($user_st->state_id == $user_data->state_id ? 'selected="selected"':'')?>><?=$user_st->state_name?> </option>
<?php }?>
</select>
</div>
<div style="margin-left:34px;">
<select style="border:0;background:black;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;" name="city" id="city-dropdown"> -->
<?php
foreach ($cities as $user_cty){ ?>
<option value="<?= $user_cty->city_id?>"<?=($user_data->cty == $user_cty->cty_id ? 'selected="selected"':'')?>><?=$user_cty->city_name?> </option>
<?php } ?>
</select>
</div>
<div style="border:0;background:none;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;">
<input type="hidden" id="old" name="old" value="<?= $user_data->img; ?>">
<img src='<?= base_url() ?>/uploads/<?= $user_data->img; ?>' style="margin-left:5px;margin-right:10px;border-radius:10px;" height="70" width="70"/>
<input type="file" name="userfile" size="20">
<div class="error">
<?php echo form_error('userfile'); ?>
</div>
</div>
<div style="margin:7px 0px 10px 35px;">
<input class="form-check-input" name ="checkbox" type="checkbox">
<label style="color : white;" class="form-check-label" for="flexCheckDefault"> I Agree with Terms & Conditions </label>
<div class="error">
<?php echo form_error('checkbox'); ?>
</div>
<div class="mb-3">
<input style="margin-left:42%;border-radius:15px; width:100px;" type="submit" value="Update" name="update" class="btn btn-success">
</div>
<?php form_close();?>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="<?php echo base_url();?>/assets/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$('#country-dropdown').on('change',function(){
//alert("inside country");
var country_id = this.value;
//console.log(country_id);
$.ajax({
url:"<?php echo base_url();?>home/fetch_state",
type:"POST",
data:{'country_id':country_id },
success:function(data){
$('#state-dropdown').html(data);
$('#city-dropdown').html('<option class="form-control" value="">Select State First</option>');
}
});
});
$('#state-dropdown').on('change',function(){
var state_id = this.value;
$.ajax({
url:"<?php echo base_url();?>home/fetch_city",
type:"POST",
data:{
state_id:state_id
},
success:function(result){
$('#city-dropdown').html(result);
}
});
});
});
</script>
</body>
</html>
用于编辑和更新数据库中数据的模型
class EditUser extends CI_Model {
public function getUser($id){
$this->db->select('*');
$this->db->where("user_id",$id);
$query = $this->db->get('user_list');
return $query->row();
}
public function updateUser($id2,$editUser){
$this->db->where('user_id',$id2);
$edit_User=[
'user_name' => $editUser['user_name'],
'fname' => $editUser['fname'],
'lname' => $editUser['lname'],
'eml' => $editUser['eml'],
'gnd'=>$editUser['gnd'],
'img'=>$editUser['img'],
'ctry'=> $editUser['ctry'],
'st' => $editUser['st'],
'cty'=>$editUser['cty'],
];
$query=$this->db->update('user_list',$edit_User);
if($query){
return true;
}else{
return false;
}
}
}
视图显示国家、州、城市,以及注册过程中使用的ajax调用,以获取国家、州、城市
class Dynamic_model extends CI_Model
{
public function fetch_country(){
$query = $this->db->get("countries");
$this->db->select('country_id','country_name');
return $query->result();
}
public function fetch_state($country_id){
$this->db->where('country_id',$country_id);
$query = $this->db->get('states');
return $query->result();
}
public function fetch_city($state_id){
$this->db->where('state_id', $state_id);
$query = $this->db->get('cities');
return $query->result();
}
}
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Edit User Details</title>
<link href="<?php echo base_url();?>/assets/css/bootstrap.min.css" rel="stylesheet">
<link href="<?php echo base_url();?>/assets/css/style.css" rel="stylesheet">
</head>
<body>
<!-- style="background:url(<?php echo base_url();?>/assets/css/Wireframe.png);" -->
<!-- <?php echo validation_errors(); ?> -->
<div class="container-sm" style="margin-top:20px;margin-bottom:20px;border:1px solid black;border-radius:10px;max-width:500px;background-color:black;border:2px solid #3498db;">
<div class="row">
<h1 style="margin-top:12px;color:white;font-weight:500;" class="text-center mb-1">Edit User Details</h1>
<?php echo form_open_multipart('home/update'); ?>
<div>
<input type="hidden" name="user_id" value="<?php echo $user_data->user_id ?>">
<input style="border:0;background:none;text-align:center;margin:14px auto;border:2px solid #3498db;border-radius:20px;width:400px;color:white;"
type="text"
value="<?= $user_data->user_name ?>"
name="username"
class="form-control"
placeholder="Username">
<div class="error">
<?php echo form_error('username'); ?>
</div>
</div>
<div>
<input style="border:0;background:none;text-align:center;margin:14px auto;border:2px solid #3498db;border-radius:20px;width:400px;color:white;"
name="email"
type="email"
value="<?= $user_data->eml ?>"
class="form-control"
placeholder="example@example.com">
<div class="error">
<?php echo form_error('email'); ?>
</div>
</div>
<div>
<input style="border:0;background:none;text-align:center;margin:14px auto;border:2px solid #3498db;border-radius:20px;width:400px;color:white;"
name="firstname"
type="text"
value="<?= $user_data->fname ?>"
class="form-control"
placeholder="First Name">
<div class="error">
<?php echo form_error('firstname'); ?>
</div>
</div>
<div>
<input style="border:0;background:none;text-align:center;margin:14px auto;border:2px solid #3498db;border-radius:20px;width:400px;color:white;"
name="lastname"
type="text"
value="<?= $user_data->lname ?>"
class="form-control"
placeholder="Last Name">
<div class="error">
<?php echo form_error('lastname'); ?>
</div>
</div>
<div style="border:0;background:none;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;">
<input style="margin-left: 10px;" class="form-check-input" type="radio" name="gender" value="Male" <?php if ($user_data->gnd=="Male") echo "checked"; ?>> Male
<input style="margin-left:20px;"class="form-check-input" type="radio" name="gender" value="Female" <?php if ($user_data->gnd=="Female") echo "checked"; ?>> Female
</div>
<div class="error">
<?php echo form_error('gender'); ?>
</div>
<div style="margin-left:34px;">
<select style="border:0;background:black;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;" name="country" id="country-dropdown" class="form-item">
<?php
foreach ($countries as $user_ctry){ ?>
<option value="<?= $user_ctry->country_id?>"<?=($user_data->ctry == $user_ctry->country_id ? 'selected="selected"':'')?>><?=$user_ctry->country_name?> </option>
<?php }?>
</select>
</div>
<div style="margin-left:34px;">
<select style="border:0;background:black;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;" name="state" id="state-dropdown" class="form-item">
<?php foreach($states as $user_st){ ?>
<option value="<?php echo $user_st->state_id?>"<?=($user_st->state_id == $user_data->state_id ? 'selected="selected"':'')?>><?=$user_st->state_name?> </option>
<?php }?>
</select>
</div>
<div style="margin-left:34px;">
<select style="border:0;background:black;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;" name="city" id="city-dropdown"> -->
<?php
foreach ($cities as $user_cty){ ?>
<option value="<?= $user_cty->city_id?>"<?=($user_data->cty == $user_cty->cty_id ? 'selected="selected"':'')?>><?=$user_cty->city_name?> </option>
<?php } ?>
</select>
</div>
<div style="border:0;background:none;margin:14px auto;padding:5px;border:2px solid #3498db;border-radius:20px;width:400px;color:white;">
<input type="hidden" id="old" name="old" value="<?= $user_data->img; ?>">
<img src='<?= base_url() ?>/uploads/<?= $user_data->img; ?>' style="margin-left:5px;margin-right:10px;border-radius:10px;" height="70" width="70"/>
<input type="file" name="userfile" size="20">
<div class="error">
<?php echo form_error('userfile'); ?>
</div>
</div>
<div style="margin:7px 0px 10px 35px;">
<input class="form-check-input" name ="checkbox" type="checkbox">
<label style="color : white;" class="form-check-label" for="flexCheckDefault"> I Agree with Terms & Conditions </label>
<div class="error">
<?php echo form_error('checkbox'); ?>
</div>
<div class="mb-3">
<input style="margin-left:42%;border-radius:15px; width:100px;" type="submit" value="Update" name="update" class="btn btn-success">
</div>
<?php form_close();?>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="<?php echo base_url();?>/assets/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$('#country-dropdown').on('change',function(){
//alert("inside country");
var country_id = this.value;
//console.log(country_id);
$.ajax({
url:"<?php echo base_url();?>home/fetch_state",
type:"POST",
data:{'country_id':country_id },
success:function(data){
$('#state-dropdown').html(data);
$('#city-dropdown').html('<option class="form-control" value="">Select State First</option>');
}
});
});
$('#state-dropdown').on('change',function(){
var state_id = this.value;
$.ajax({
url:"<?php echo base_url();?>home/fetch_city",
type:"POST",
data:{
state_id:state_id
},
success:function(result){
$('#city-dropdown').html(result);
}
});
});
});
</script>
</body>
</html>
编辑用户详细信息
>
>
-->
>