2013-04-08 29 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