douzhulan1815 2015-12-21 02:50
浏览 47
已采纳

插入具有相同文本字段值的多个复选框值(codeigniter)

I'm trying to put values into a MySQL table like this one:

|---sbjct_name---|---level---|

|---------- Physics ----------|----Level 1-----|

|---------- Physics ----------|----Level 2-----|

|---------- Physics ----------|----Level 3-----|

|---------- Calculus ---------|----Level 1-----|

|---------- Calculus ---------|----Level 2-----|

(I don't know how to make table but I guess u get the idea)

Let say I'm going to put "Math" into sbjct_name column alongside with "Level 1", "Level 2", "Level 3" values into the level column.

The basic idea is from this pict (selecting value for a same input text value).

Instead of inserting "Math" multiple times for each level, I'm trying to make it one time submit with checkbox tag. It will be something like this pict (with checkbox).

I don't have any idea how to make this happen (or if it possible in CI).

This is some code from my Controller,

public function add_subject()
{
    $data = array();

    if ($this->input->post('savebttn'))
    {
        $this->form_validation->set_rules('sbjct_name', 'Subject Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('level', 'Level', '|required|is_natural');
        $this->form_validation->set_error_delimiters('<span class="fielderror">','</span>');

        if ($this->form_validation->run() == FALSE)
        {
            $data['reset'] = FALSE;
        }
        else
        {
           //I don't know what to do here
           //I don't know what to do here
           //I don't know what to do here
           //I don't know what to do here
        }
    }
    $data['level'] = $this->admin_model->get_checkbox_option('level', 'lvl_id', 'lvl_name');
    $data['page'] = 'createsubject';
    $this->load->view('admin/main', $data);
}

This is a checkbox view function in the Model,

public function get_checkbox_option($table, $id, $name, $selected=0)
{
    $query = $this->db->get($table);
    $select= '';
    if ($query->num_rows() > 0)
    {
        foreach ($query->result_array() as $row)
        {
            $selected_option = ($selected == $row[$id]) ? 'selected = "selected" ':' ';
            $select .='<input type="checkbox" name="level" value="'.$row[$id].'" '.$selected_option.'>'.$row[$name].'<br>';
        }
    }
    return $select;
}

This is the form on the View,

    <form action="" method="post" id="createcategoryform">
        <table>
            <tbody>
                <tr>
                    <td><div class="label">Subject Name</div></td>
                    <td><div class="input">
                        <input type="text" name="sbjct_name" size="39" class="ui-corner-all input-text validate[required]">
                        <?=form_error('sbjct_name');?>
                    </div></td>
                </tr>
                <tr>
                    <td><div class="label">Level</div></td>
                    <td><div class="input-text ui-corner-all validate[required]">
                        <?=(isset($level)) ? $level: '';?>
                    </div><?=form_error('level');?></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" value="Save" name="savebttn" class="input-button ui-corner-all ui-state-default"></td>
                </tr>
            </tbody>
        </table>
    </form>

I will be grateful if you can give any clue. :D

  • 写回答

1条回答 默认 最新

  • dongtu1357 2015-12-23 07:07
    关注

    first

    make your checkbox as an array so it look like this

    $select .='<input type="checkbox" name="level[]" value="'.$row[$id].'" '.$selected_option.'>'.$row[$name].'<br>';
    

    give attention to the name. yours name="level" and mine name="level[]" , then you can grab all level as one variable

    second

    make your controller like this

    if ($this->form_validation->run() == FALSE)
            {
                $data['reset'] = FALSE;
            }
            else
            {
              // make array container for input batch
              $insertData = array();
              if(!empty($this->input->post('level'))) {
                  foreach($this->input->post('level') as $level) {
                        $tempArray = array(
                          'sbjct_name' => $this->input->post('sbjct_name'),
                          'level' => $level
                        );
    
                        array_push($insertData, $tempArray);
                  }
    
                  // it's better to put this in model
                  // but for example purpose I put it there
                  $this->db->insert_batch('table', $insertData);
    
                  //do what you want to do here
              }
            }
    

    hope you get the point @nasamikhail

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵