dimanche 28 août 2016

Post Multiple Checkbox from Table To Database Using Codeigniter

I've the problem to post my checkbox to database. Please help me...

Here is my view :

<table>
    <tr>
        <td>
            <input type='hidden' name='userid[]' value='1'>
            <input type='text' name='username[]' value='username1'> 
        </td>
        <td>
            <input type='checkbox' name='as_admin[]' value=1>
        </td>
    </tr>
    <tr>
        <td>
            <input type='hidden' name='userid[]' value='2'>
            <input type='text' name='username[]' value='username2'> 
        </td>
        <td>
            <input type='checkbox' name='as_admin[]' value=1>
        </td>
    </tr>
</table>

This is my controller :

$this->Model_user->insert_user();

And this is my model :

function insert_user(){
    $user_count = count($this->input->post('userid'));
    $userid     = $this->input->post('userid');
    $username   = $this->input->post('username');
    $as_admin   = $this->input->post('as_admin');

    for ($i=0; $i < $user_count; $i++){
        $info_user = array(
            'user_id'   => $userid[$i],
            'user_name' => $username[$i],
            'as_admin'  => $as_admin[$i],
        );
        $this->db->insert($info_user);
    }
}

And the problem is when 'username2' mark as admin (second row checkbox checked), in the database will be like this :

|user_id|user_name|as_admin|
|   1   |username1|   1    |
|   2   |username2|   0    |

it should be like this :

|user_id|user_name|as_admin|
|   1   |username1|   0    |
|   2   |username2|   1    |

Does anyone now how to save those thing, Please...

Thanks in advance...




Aucun commentaire:

Enregistrer un commentaire