gpt4 book ai didi

php - 如何连接数组元素并将它们存储在 codeigniter php 中的字段中

转载 作者:行者123 更新时间:2023-11-28 23:54:58 24 4
gpt4 key购买 nike

我正在为一个项目使用 PHP Codeigniter,我需要连接多个字段并创建一个名为 key 的新字段,该字段应存储在同一个表中。例如,如果我插入 name:jack country:USA code:+691 那么我的代码应该动态地将 JACK-USA-691 存储到我的表的关键字段中。我有一个执行我的 CRUD 操作的基本模型。下面是我获得新用户(经销商)的 Controller 代码,以及我获得新用户(经销商)的模型:

Controller :

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 并通过连接字段来更新它。我也觉得它是对的,但如何连接数组的元素?以及如何动态地做到这一点?请帮助我的代码!我可以将串联的字符串动态存储在我的数据库中吗?

最佳答案

使用 implode() 连接如下

$values=array($this->input->post('name'),$this->input->post('country'),$this->input->post('name'));
$key=implode('-',$value);

//output will be name-country-code

关于php - 如何连接数组元素并将它们存储在 codeigniter php 中的字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31935658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com