如果在插入批处理 CodeIgniter 中输入不为空,如何发送值
How to send value if input not empty in insert batch CodeIgniter
基本上我有这个将输入的值发送到数据库:
$tag = $this->input->post('tags[]');
$data = array();
foreach ($tag as $term){
$data[] = array(
'post_id' => $post_id,
'term_id' => $term,
'user_id' => $this->input->post('user_id'),
'status' => 'attached',
'type' => 'tag',
);
}
$this->db->insert_batch('ci_relationship', $data);
但我试图通过让管理员输入 X 用户作为值或他的会话 user_id 来创建 user_id "optional",以防他决定离开它空..
我试过了,但还是不行:
$tag = $this->input->post('tags[]');
$data = array();
foreach ($tag as $term){
$data[] = array(
'post_id' => $post_id,
'term_id' => $term,
//'user_id' => $this->input->post('user_id'),
'status' => 'attached',
'type' => 'tag',
);
}
$user_id = $this->input->post('user_id');
if(empty($user_id)){
$data['user_id'] = $this->session->userdata('user_id');
} else {
$data['user_id'] = $user_id;
}
$this->db->insert_batch('ci_relationship', $data);
如您所见,我在 foreach 中注释了 user_id,然后将其添加到下一个代码块中,该代码块应该检查输入是否为空。
提前致谢!
那个 if 语句必须在那个 foreach 里面。
$tag = $this->input->post('tags[]');
$user_id = $this->input->post('user_id');
$data = array();
foreach ($tag as $key => $term){
$data[$key] = array(
'post_id' => $post_id,
'term_id' => $term,
'status' => 'attached',
'type' => 'tag',
);
if(empty($user_id)){
$data[$key]['user_id'] = $this->session->userdata('user_id');
} else {
$data[$key]['user_id'] = $user_id;
}
}
$this->db->insert_batch('ci_relationship', $data);
我认为不需要添加更多行,只需在旁边执行此操作即可
$data[] = array(
'post_id' => $post_id,
'term_id' => $term,
'user_id' => (!empty($this->input->post('user_id')) ? $this->input->post('user_id') : '',
'status' => 'attached',
'type' => 'tag',
);
$tag = $this->input->post('tags[]');
$data = array();
foreach ($tag as $key => $term){
if(empty($this->input->post('user_id')))
{
$data[$key] = array(
'post_id' => $post_id,
'term_id' => $term,
'status' => 'attached',
'type' => 'tag',
);
}
else
{
$data[$key] = array(
'post_id' => $post_id,
'term_id' => $term,
'user_id' => $this->input->post('user_id'),
'status' => 'attached',
'type' => 'tag',
);
}
}
$this->db->insert_batch('ci_relationship', $data);
希望对你有用。
基本上我有这个将输入的值发送到数据库:
$tag = $this->input->post('tags[]');
$data = array();
foreach ($tag as $term){
$data[] = array(
'post_id' => $post_id,
'term_id' => $term,
'user_id' => $this->input->post('user_id'),
'status' => 'attached',
'type' => 'tag',
);
}
$this->db->insert_batch('ci_relationship', $data);
但我试图通过让管理员输入 X 用户作为值或他的会话 user_id 来创建 user_id "optional",以防他决定离开它空..
我试过了,但还是不行:
$tag = $this->input->post('tags[]');
$data = array();
foreach ($tag as $term){
$data[] = array(
'post_id' => $post_id,
'term_id' => $term,
//'user_id' => $this->input->post('user_id'),
'status' => 'attached',
'type' => 'tag',
);
}
$user_id = $this->input->post('user_id');
if(empty($user_id)){
$data['user_id'] = $this->session->userdata('user_id');
} else {
$data['user_id'] = $user_id;
}
$this->db->insert_batch('ci_relationship', $data);
如您所见,我在 foreach 中注释了 user_id,然后将其添加到下一个代码块中,该代码块应该检查输入是否为空。
提前致谢!
那个 if 语句必须在那个 foreach 里面。
$tag = $this->input->post('tags[]');
$user_id = $this->input->post('user_id');
$data = array();
foreach ($tag as $key => $term){
$data[$key] = array(
'post_id' => $post_id,
'term_id' => $term,
'status' => 'attached',
'type' => 'tag',
);
if(empty($user_id)){
$data[$key]['user_id'] = $this->session->userdata('user_id');
} else {
$data[$key]['user_id'] = $user_id;
}
}
$this->db->insert_batch('ci_relationship', $data);
我认为不需要添加更多行,只需在旁边执行此操作即可
$data[] = array(
'post_id' => $post_id,
'term_id' => $term,
'user_id' => (!empty($this->input->post('user_id')) ? $this->input->post('user_id') : '',
'status' => 'attached',
'type' => 'tag',
);
$tag = $this->input->post('tags[]');
$data = array();
foreach ($tag as $key => $term){
if(empty($this->input->post('user_id')))
{
$data[$key] = array(
'post_id' => $post_id,
'term_id' => $term,
'status' => 'attached',
'type' => 'tag',
);
}
else
{
$data[$key] = array(
'post_id' => $post_id,
'term_id' => $term,
'user_id' => $this->input->post('user_id'),
'status' => 'attached',
'type' => 'tag',
);
}
}
$this->db->insert_batch('ci_relationship', $data);
希望对你有用。