使用Codeigniter从checkbox数组将数据插入数据库 [英] Insert data into the database from checkbox array using Codeigniter

查看:130
本文介绍了使用Codeigniter从checkbox数组将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Codeigniter从checkbox [array]插入数据到数据库。
我的case是用户检查assesmen条件基础从复选框形式,然后我的Controller处理它,当复选框是检查,然后值是1,如果取消选中然后0.这是我的观点

I want to insert data into the database from checkbox[array] using Codeigniter. My case is User check assesmen criteria base from checkbox form, then my Controller process it when checkbox is check then value is 1, if uncheck then 0. Here is my view

ID | Trimester I | Trimester II | Trimester III |
21 | checbox_trimester_i[] | checbox_trimester_ii[] | checbox_trimester_iii[]
42 | checbox_trimester_i[] | checbox_trimester_ii[] | checbox_trimester_iii[]
23 | checbox_trimester_i[] | checbox_trimester_ii[] | checbox_trimester_iii[]

我想插入表'assesmen'。这是我在DB中的列

I want to insert into table ‘assesmen’. Here is my column in DB

ID | ID_pk | Trimester_1 | Trimester_2 | Trimester_3 |
1 | 21 | 1 | 0 | 1
2 | 42 | 0 | 1 | 1
3 | 23 | 1 | 0 | 0

注意:

ID_pk = ID
Trimester_1 => checbox_trimester_i[]
Trimester_2 => checbox_trimester_ii[]
Trimester_3 => checbox_trimester_iii[]

这是我的查看表单:

foreach ($kuisioner as $art) { $no++; ?>
   <tr>
        <td><input type="hidden" name="id[]" value="<?php echo $art->id ?>"></td>
        <td align="center">
            <input type="checkbox" name="checbox_trimester_i[]">
        </td>         
        <td align="center">
            <input type="checkbox" name="checbox_trimester_ii[]">
        </td>         
        <td align="center">
            <input type="checkbox" name="checbox_trimester_iii[]">
        </td>
        </tr>
     <?php }

ScreenShoot View

ScreenShoot View

表单视图

Form View

这里是我的Controller,但是还是不能工作,请帮我解决吧。感谢

Here is my Controller, but still not work, please help me to solve it. Thanks 

$posted = $this->input->post();
$as_trim_satu = isset($posted['checbox_trimester_i']) ? $posted['checbox_trimester_i'] : array();
$data = array();
  foreach($posted['id'] as $key => $value)
  {
    $info_user = array(
    'user_id' => $value,
    'trimester_satu' => in_array($posted['id'][$key], $as_trim_satu) ?1:0,
    );
    $data[] = $info_user;
  }
    echo "<pre>";
    print_r($data);
    echo "</pre>";


推荐答案

您可能需要如下调整视图正确的值插入控制器。如果是这样,你可以使用下面的代码。

You may need to adjust your view like below to get correct values into the controller. If so, you can use the below code.

输入

name="checbox_trimester_i[<?php echo $art->id ?>]"

更新。

<?php foreach ($kuisioner as $art) { $no++; ?>
<tr>
    <td><input type="hidden" name="id[]" value="<?php echo $art->id ?>"> </td>
    <td align="center">
        <input type="checkbox" name="checbox_trimester_i[<?php echo $art->id ?>]" value="1">
    </td>         
    <td align="center">
        <input type="checkbox" name="checbox_trimester_ii[<?php echo $art->id ?>]"  value="1">
    </td>         
    <td align="center">
        <input type="checkbox" name="checbox_trimester_iii[<?php echo $art->id ?>]" value="1">
    </td>
    </tr>
 <?php } ?>

以下是访问从页面发布的表单数据的控制器代码

Below is controller code to access the form data posted from page

if($this->input->post()){
        $data = array();
        $ids = $this->input->post('id');
        $checbox_trimester_i = $this->input->post('checbox_trimester_i');
        $checbox_trimester_ii = $this->input->post('checbox_trimester_ii');
        $checbox_trimester_iii = $this->input->post('checbox_trimester_iii');   
        if($ids ){
            foreach ($ids as $key => $value){
                $info_user = array(
                'user_id' => $value,
                'Trimester_1' => !empty($checbox_trimester_i[$value]) ? 1: 0,
                'Trimester_2' => !empty($checbox_trimester_ii[$value]) ? 1: 0,
                'Trimester_3' => !empty($checbox_trimester_iii[$value]) ? 1: 0,
                );
                $data[] = $info_user;

            }   
        }

        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }

请注意变量 $ info_user 我创建了。在更新之前,可以使用正确的数据库列名称。

Please note the variable $info_user I have created. You may use your correct db column names before you update.

这篇关于使用Codeigniter从checkbox数组将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆