2013-08-22 30 views
0

我這裏有一個表格:http://kees.een-site-bouwen.nl/evenementform如何在addform中使用可選字段?

,你可以看到我有一個+一個按鈕來進行第二次約會添加到事件。所有工作時,當我填寫所有內容(所以日期字段),當我離開可選的空白我得到一個錯誤,因爲它是空的。

我嘗試了一些使用這些可選字段,只有當他們填寫,但我不知道如何。

我想我必須使用一個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'); 
} 

如何讓第二個日期字段可選的,所以它跳過他們時,他們是空的?

回答

0

嘗試這樣:

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]));  
    } 

}; 
+0

仍然沒有工作。我得到這個錯誤:錯誤號:1292 錯誤的日期值:''在第1行的'datum'列' INSERT INTO'agendadatum'('idagenda','datum','van','tot ')VALUES(26,'','','')' –

+0

EDITED上面的代碼請再試一次。 @KeesSonnema –

+0

是的工作,爲什麼我不能與這個我自己大聲笑。謝謝 –

相關問題