samedi 17 septembre 2016

Insert data into the database from checkbox array using Codeigniter

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[]

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

Note * :

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

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

for ($i=0; $i < count($id); $i++) { 
    $check_0 = isset($_POST['checbox_trimester_i'][$i]) ? 1 : 0;
    $check_1 = isset($_POST['checbox_trimester_ii'][$i]) ? 1 : 0;
    $check_2 = isset($_POST['checbox_trimester_iii'][$i]) ? 1 : 0;

    $arrayName = array(
        'id' => $cek[$id], 
        'trimester_1' => $check_0,
        'trimester_2' => $check_1,
        'trimester_3' => $check_2,
    );

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




Aucun commentaire:

Enregistrer un commentaire