我正在使用PHP Codeigniter进行项目,我需要连接多个字段并创建一个名为key的新字段,该字段应存储在同一个表中。例如,如果我插入名称:jack country:USA代码:+691那么我的代码应该将JACK-USA-691动态存储到我的表的关键字段中。我有一个执行我的CRUD操作的基本模型。下面是我的控制器代码,以获取新用户(经销商)以及我获得新用户(经销商)的模型:
控制器:
public function edit ($id = NULL)
{
// Fetch a user or set a new one
if ($id) {
$this->data['user'] = $this->reseller_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else {
$this->data['user'] = $this->reseller_m->get_new();
}
// Set up the form
$rules = $this->reseller_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email','password','phone','balance','user_num','address','country','created','modified','status'));
$data['password'] = $this->reseller_m->hash($data['password']);
$values=array($this->input->post('name'),$this->input->post('country'),$this->input->post('name'));
$key=implode('-',$values);
$this->db->insert('reseller',$key);
$key=$this->reseller_m->save($data, $id);
for($i=1; $i<=$data['user_num'];$i++)
{
$userdata=array('key'=>$key);
// here users is taken name of user table with retailer_id is field
$this->user_m->save($userdata,$id);
}
redirect('admin/reseller');
}
// Load the view
$this->data['subview'] = 'admin/reseller/edit';
$this->load->view('admin/_layout_main', $this->data);
}
模特:
public function get_new(){
$user = new stdClass();
// $user->id = '';
$user->sip_username='';
$user->sip_password='';
$user->key='';
$user->allocation_block='';
$user->name='';
$user->email = '';
$user->password = '';
$user->phone='';
$user->user_num='';
$user->address = '';
$user->status = '';
$user->country='';
$user->created = '';
$user->modified = '';
$user->balance = '';
return $user;
$this->session->set_userdata($data);
}
我的一个解决方案是创建一个用户获取其id并通过连接字段来更新它。我也发现它是正确的,但如何连接数组的元素?以及如何动态地这样做?请帮我处理我的代码!我可以动态地将连接的字符串存储在我的数据库中吗?
答案 0 :(得分:1)
使用implode()
连接如下
$values=array($this->input->post('name'),$this->input->post('country'),$this->input->post('name'));
$key=implode('-',$value);
//output will be name-country-code