如何为表单设置DB代码


How to set up DB codes for forms

我有一个有39个复选框的页面。我的示例中的复选框类似于表单名称。我的问题是,有39个复选框,我需要一种方式来存储什么形式给学生。目前我所设置的是,每个表单都用逗号和引号分开,以便在运行报告时,管理员可以使用CSV下载选项和学生收到的表单组。这是工作的,但是非常初级的,也给了一个坏的影响,在每个表单名之前/是存在的,因为mysql转义引号。

这是我目前拥有的:

 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个表单)

听起来你需要一个学生和表单之间的一对多关系。我想对这个话题做点调查。

我认为在数据库的单个字段中存储逗号分隔的值通常是非常糟糕的形式,如果你这样做,它几乎总是一个信号,你需要(至少)另一个表

我花了一两个小时重构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   } 
?>

谢谢你的好主意!