2013-04-08 28 views
0

我有一個包含39個複選框的頁面。我的例子中的複選框類似於表單名稱。我的問題是,有39個複選框,我需要一種方法來存儲給學生的表單。目前我設置的是每個表單用逗號和引號分隔,這樣當報表運行時,管理員可以使用CSV下載選項和學生收到的組。這種方法很有效,但是很簡單,並且在每個表單名稱前都存在一個不好的副作用,因爲mysql會導出引號。如何爲表單設置DB代碼

這是我目前有:

if ($this->input->post('action') == 'additional') { 
    $givenforms = ""; 

    foreach ($this->input->post('form') as $forms) { 
     $givenforms .= ', "' . $forms . '"'; 
     } 
     $comments = 'This student was given' . $givenforms . ''; 

     if (($this->input->post('action') == 'additional') && ($this->input->post('other') == 'OTHER')) { 
      $comments .= ', '.$this->input->post('counselorcomments'); 
     } 
} 
在數據庫中的結果會像

還是那句話:這名學生被賦予 「XYZ」, 「EOE」, 「WWO」,

差不多我只需要關於如何儲存學生給定的表格的想法,如果需要的話,如果所有39個表格都給予學生,我需要存儲學生給出的所有表格以供稍後報告。 (儘管不會給出39種形式)

回答

0

重構我用CSV獲得的回報的一兩個小時得到了很好的回報。我對已知信息的報告/分析可能性以及現在存儲的方式非常滿意。

其他任何人正在尋找這樣的事情的代碼片段! :

if ($this->form_validation->run() == FALSE) { // This stuff is self explanatory RT(F)M if you will :) 
    $this->cont(); 
} else { 
    $this->load->model('queue_model'); // Load model 

    $session = $this->uri->segment(3); // Gets the session id 
    $counselor = $this->session->userdata('username'); // I get counsellor names from the username they log in by joining between the two tables 

     if ($this->input->post('action') == 'Additional') { // If additional forms is checked do the following 

     foreach ($this->input->post('form') as $form_id) { // for each form submitted take the session Id from above and insert it into the table forms with the foreach $form_id variable 
      $this->queue_model->forms($session, $form_id); 
     } 

     if (($this->input->post('action') == 'Additional') && ($this->input->post('addother') == 'addotherinfo')) { // If forms were submitted and a addotherinfo was [checked] add comments 
      $comments = ''.$this->input->post('action'). ' - '.$this->input->post('counselorcomments').''; 
     } else { 
      $comments = $this->input->post('action'); 
     } 
} 

的形式表還增加(與ID和表單名稱)讓我動態讓複選框,像這樣:

<?php 
     foreach ($elevennine as $form) { ?> 
       <label id="form"><input type="checkbox" name="form[]" value="<?php echo $form['form_id'] ?>" <?php echo set_checkbox('form', $form['form_id']) ?>><?php echo $form['forms'] ?></label> 
<?php } 
?> 

感謝所有的好點子!

1

聽起來像你需要一個:很多學生和形式之間的關係。可能想對這個話題做一點研究。

我認爲在數據庫的單個字段中存儲逗號分隔值通常很糟糕,如果你這樣做,它幾乎總是一個表示你需要(至少)另一個表的標誌。

+0

在單個字段中使用逗號分隔的值,然後通過分隔符將其分解以在PHP中創建數組是一種解決方案,但正如jcorry所說的那樣是一種糟糕的設計。 不過,這是一個解決方案;如果你知道這會限制你的應用程序的靈活性,並且擁有多對多的中間表是過度的。 – 2013-04-08 07:55:09

+0

絕對正確我不喜歡這樣的設計,只是我的問題是,而不是如何設置它(我對數據庫模式和關係非常熟悉),當我設置表格1-39如果一個學生被給予表格1-10,我應該如何在學生表中標記該表格?這就是讓我困惑的原因。我不知道如何使它與同一列和行中的花葯字段相關的多個數字數字。我會進一步研究這件事情! – 2013-04-08 10:18:16

+0

您可以將表單和學生之間的關聯存儲在另一個表中:students_forms。該表作爲id,student_id,form_id。每個學生表格都有一行。你可以在表格上給學生加入表格。您應該對這種技術非常熟悉,因爲它對於數據庫設計來說非常重要。 – jcorry 2013-04-08 10:25:57