我这里有一个表格:http://kees.een-site-bouwen.nl/evenementform
如您所见,我有一个带有 +
的按钮,用于向事件添加第二个日期。当我填写所有内容(因此两个日期字段)时,一切正常,当我将可选字段留空时,我会收到错误,因为它是空的。
我尝试了一些方法,仅在填写这些可选字段时才使用它们,但我不知道如何操作。
我想我必须使用 foreach 循环。
我将字段插入数据库的代码:
public function addevenement($image_data = array())
{
$data = array(
'titel' => $this->input->post('titel'),
'waar' => $this->input->post('waar'),
'entree' => $this->input->post('entree'),
'organisatie' => $this->input->post('organisatie'),
'omschrijving' => $this->input->post('omschrijving'),
'www' => $this->input->post('www'),
'tel' => $this->input->post('tel'),
'email' => $this->input->post('email'),
'afbeelding' => $image_data['file_name']
);
$this->db->insert('agenda', $data);
$id = $this->db->insert_id();
$data2 = array(
'idagenda' => $id,
'datum' => $this->input->post('datum'),
'van' => $this->input->post('van'),
'tot' => $this->input->post('tot')
);
$this->db->join('agenda', 'agendadatum.idagenda = agenda.idagenda');
$this->db->where('agendadatum.idagenda', $id);
$post = $this->input->post();
for ($i = 0; $i < count($post['datum']); $i++) {
$this->db->insert('agendadatum', array('idagenda' => $id, 'datum' => $post['datum'][$i], 'van' => $post['van'][$i], 'tot' => $post['tot'][$i]));
};
redirect('agenda');
}
如何使第二个日期字段可选,以便在它们为空时跳过它们?
最佳答案
尝试这样的事情:
for ($i = 0; $i < count($post['datum']); $i++) {
if( $post['datum'][$i] != "" ){
$this->db->insert('agendadatum', array('idagenda' => $id, 'datum' => $post['datum'][$i], 'van' => $post['van'][$i], 'tot' => $post['tot'][$i]));
}
};
关于php - 如何在addform中使用可选字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18374655/